You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Right now, the only way to respond to "outside world" events is to leave the React's event system and add a native DOM listener. This is bad, since it will require more mental overhead when you have to work with this (you need to think about your event listener receiving a native event, or a react synthetic event). It will also simply not be possible for computed SyntheticEvents (e.g. onChange).
It also makes it very hard for react events handlers to interrupt the DOM handlers. Consider the following example, where it's not intuitive why the React listener can not stop propagation to the document. (Spoiler: React also listens on document, that's why you'd have to use SyntheticEvent#nativeEvent.stopImmediatePropagation():
varExample=React.createClass({render: function(){return(<divonKeyDown={function(e){e.stopPropagation()}}/>);}});document.addEventListener("keydown",function(){alert('why does this still fire?');});ReactDOM.render(<Examplename="react"/>,document.getElementById('react'));
An example for when you want to deal with outside events is a simple drawing tool, that must listen on keyup to stop the drawing process.
To solve this problem, I propose a new public API, something like an EventRoot. It should behave like a regular DOM Node, so that you can addEventListener() and removeEventListener(), but it's callbacks will receive the SyntheticEvent. It will get called when triggering a two phase dispatch. It respects the capture and bubble order as well as stopPropagation(). Everything you'd expect when listening on document.
For the above example, you'd only have to replace document with the new event root. The stopPropagation() can now correctly be applied.
The text was updated successfully, but these errors were encountered:
Thanks for the writeup @philipp-spiess, I agree that there may be some utility in providing such an API. We've had discussion about this in the past, see #285. Would you mind posting there instead?
I'm going to close this so we don't fragment the discussion, but I encourage you to check out #285 and post your proposal there!
This is a feature request.
Right now, the only way to respond to "outside world" events is to leave the React's event system and add a native DOM listener. This is bad, since it will require more mental overhead when you have to work with this (you need to think about your event listener receiving a native event, or a react synthetic event). It will also simply not be possible for computed
SyntheticEvent
s (e.g.onChange
).It also makes it very hard for react events handlers to interrupt the DOM handlers. Consider the following example, where it's not intuitive why the React listener can not stop propagation to the
document
. (Spoiler: React also listens ondocument
, that's why you'd have to useSyntheticEvent#nativeEvent.stopImmediatePropagation()
:An example for when you want to deal with outside events is a simple drawing tool, that must listen on
keyup
to stop the drawing process.To solve this problem, I propose a new public API, something like an
EventRoot
. It should behave like a regular DOM Node, so that you canaddEventListener()
andremoveEventListener()
, but it's callbacks will receive theSyntheticEvent
. It will get called when triggering a two phase dispatch. It respects thecapture
andbubble
order as well asstopPropagation()
. Everything you'd expect when listening ondocument
.For the above example, you'd only have to replace
document
with the new event root. ThestopPropagation()
can now correctly be applied.The text was updated successfully, but these errors were encountered: