question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Conditional rendering

See original GitHub issue

Hi

I reviewed branch next and decided that I will add features mentioned in PRs #41 and #42 there as it makes more sense. You addressed already disabling on function and passing data to the onClick.

Therefore I have few items left to add:

  • visible prop in Item which is either boolean or function where, function has access to passed data (use case: some items exists only for specific data)
  • toggleContextMenu as a function, when you need create context without new DOM node (use case: I want div which is draggable and is also context menu provider)

Please proceed with next branch.

Thanks

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
ronencommented, May 18, 2019

In case anyone’s interested, I was able to implement conditional rendering with the current API, it came out pretty clean (IMHO). Here’s a sketch:

import { Item, Menu, contextMenu } from 'react-contexify'
import uniqueId from 'lodash/uniqueId'

class MyContextMenu extends PureComponent {

  constructor (props) {
    super(props)
    this.menuId = uniqueId('MyContextMenu')
    this.state = {}
  }

  open (event) {
    event.preventDefault()
    const a = ... // compute value based on event and this.props
    const b = ... // compute value based on event and this.props
    this.setState({ a, b })
    contextMenu.show({ id: this.menuId, event })
  }

  render () {
    const { a, b } = this.state
    return <Menu className='my-menu' id={this.menuId} >
       { a && <Item ...>This is item A</Item> }
       { b && <Item ...>This is item B</Item> }
    </Menu>
  }

  // ...click handlers here...
}

I’m using react-redux, so I wrap that in a connect(mapStateToProps) for the menu to depend on the app state, and for the click handlers to be able to dispatch()

Invoke the context menu with a ref:

class Caller extends Component {
   constructor (props) {
     super(props)
     this.menuRef = React.createRef()
  }

  render () {
    return <div className='Caller'>
      <div onContextMenu={event => this.menuRef.current.open(event)}>
        ...context menu available here..
      </div>
      <MyContextMenu ref={this.menuRef} otherProps.../>
   </div>
  }

}

This has the nice side-effect (IMHO) that the caller is completely insulated from the react-contextify API.

0reactions
fkhadracommented, May 17, 2019

Hello @ronen,

Your implementation is 👌. I’ll close the issue.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Conditional Rendering - React
In React, you can create distinct components that encapsulate behavior you need. Then, you can render only some of them, depending on the...
Read more >
React Conditional Rendering - W3Schools
Another way to conditionally render a React component is by using the && operator. Example: We can embed JavaScript expressions in JSX by...
Read more >
React conditional rendering: 9 methods with examples
In React, conditional rendering refers to the process of delivering elements and components based on certain conditions.
Read more >
7 Ways to Implement Conditional Rendering in React ...
Conditional rendering is a term to describe the ability to render different user interface (UI) markup if a condition is true or false....
Read more >
Six methods to achieve conditional rendering in React - Flexiple
What is Conditional Rendering? ... While developing an application in React or any other JS library/ framework, it is a common use case...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found