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
{{ message }}
Repository navigation
Detecting 'outside' events using the same logic as with portals #10962
I was surprised (pleasantly so) to read that events bubble through portals and up the component tree rather than the DOM tree. Obviously this means I can detect clicks that occur within my component or child components without having to worry about whether they happen to be portal hosted.
However, another fairly common thing you may want to do is detect whether an event occurred outside of a component or child components. There are plenty of libraries out there that do this, but they all (AFAIK) use the DOM tree so don't have this same behaviour.
I was curious whether or not there is anything new, but unadvertised, in React 16 that can help with this part of the puzzle?
There isn't anything new specifically for this. events in portals sort of "just work" due to how they are implemented, not because there is anything new in the event code. The way it works in a nutshell is that when React sees an event at the top level (on document) it finds the component associated with that events target and then manually walks up and down the component tree triggering event handlers, mimicking the capture and bubble phases of real DOM events. Previously subtree rendering (old portals) were different component trees so react couldn't "see" events between the portal and parent, now Portals are properly part of the same react component tree as their parent, so when React walks down and up to fire events it can now cross portal boundaries
I have some code that tries to do this using the existing event system. I'll leave it here in case anyone happens to find it useful, but it is a bit messy:
Activity
jquense commentedon Sep 29, 2017
There isn't anything new specifically for this. events in portals sort of "just work" due to how they are implemented, not because there is anything new in the event code. The way it works in a nutshell is that when React sees an event at the top level (on
document) it finds the component associated with that eventstargetand then manually walks up and down the component tree triggering event handlers, mimicking the capture and bubble phases of real DOM events. Previously subtree rendering (old portals) were different component trees so react couldn't "see" events between the portal and parent, now Portals are properly part of the same react component tree as their parent, so when React walks down and up to fire events it can now cross portal boundariesjamiewinder commentedon Sep 29, 2017
Thanks for the info.
I have some code that tries to do this using the existing event system. I'll leave it here in case anyone happens to find it useful, but it is a bit messy:
Usage:
Basically, if we detect the event before it reaches
windowthen we know it was internal. If not, it was external.Unfortunately it:
gaearon commentedon Oct 3, 2017
There’s nothing new on that front, but you can follow #285 in case we add something.
amanmahajan7 commentedon Dec 5, 2018
I ended up using the following solution if it helps anyone. Here is an explanation of the approach