See my comment above.
Basically, you cannot rely on the ability to stop distributing events like onClick in React when using jquery (or regular JS events) in combination.
What you can do is check your $('body').click() function, which you donβt click on the menu (so it doesnβt close your menu after clicking on the item). It is also worth noting that you should bind this INSIDE body event to a reaction component so that you can easily add / remove it only by installing the component and not change the functionality of the component so that the component itself knows nothing about.
So what you are doing is something like this (nb: unverified code, but based on the component that I have with the same problem)
var Menu = React.createClass({ getInitialState: function() { return { show: true } }, componentDidMount: function() { $('body').bind('click', this.bodyClickHandler); }, componentWillUnmount: function() { $('body').unbind('click', this.bodyClickHandler); }, bodyClickHandler: function(e) { if ($(e.target).parents('div').get(0) !== this.getDOMNode()) { this.setState({ show: false }) } }, clickHandler: function(e) {
So, in the above code, we assume that you want to display a drop-down menu as soon as the component is installed in dom. This may not be the case, since you probably want to mount the component and then show / hide it based on a hang. In this case, you just need to move the event binding of $('body').bind() from componentDidMount to the callback wherever you display the menu. $('body').unbind() should probably be moved if the component is hidden. Then you get the ideal situation when, when the menu is displayed, we bind the body event, waiting for a click, and turn it off whenever it is hidden and / or disabled.
What happens when bodyClickHandler is called before clickHandler when a menu is clicked, however bodyClickHandler only hides the menu if you clicked the parent tree, did not contain the root div our component ( this.getDOMNode() returns the html element of the root node inside render() return).
As a result, clicking outside the component will close it, clicking inside the component will do nothing, clicking <li> will trigger clickHandler . Everything works as expected.
Hope this helps.
Mike Driver Sep 16 '14 at 12:54 on 2014-09-16 12:54
source share