React uses event delegation with a single event listener per document for events that pop up, such as a โclickโ in this example, which means stopping propagation is not possible; the real event is already spreading by the time you interact with it in React. stopPropagation in a React artificial event is possible because React handles the propagation of artificial events on its own.
JSFiddle work with corrections from below.
Respond to the propagation of a stop signal at a jQuery event
Use Event.stopImmediatePropagation to prevent other listeners (in this case jQuery) from being called in the root directory. It is supported in IE9 + and modern browsers.
stopPropagation: function(e){ e.stopPropagation(); e.nativeEvent.stopImmediatePropagation(); },
- Caution: listeners are called in the order in which they are connected. React must be initialized before other code (here jQuery) for this to work.
jQuery Stop React Propagation
Your jQuery code also uses event delegation, which means that calling stopPropagation in the handler does not stop anything; the event is already passed to document , and the React Listener will be fired.
// Listener bound to 'document', event delegation $(document).on('click', '.stop-propagation', function(e){ e.stopPropagation(); });
To prevent propagation outside the element, the listener should be attached to the element itself:
// Listener bound to '.stop-propagation', no delegation $('.stop-propagation').on('click', function(e){ e.stopPropagation(); });
Edit (2016/01/14): Clarified that delegation is necessarily used only for events that pop up. For more information on event handling, the React source has descriptive comments: ReactBrowserEventEmitter.js .
Ross Allen Jun 26 '14 at 3:19 2014-06-26 03:19
source share