[ClickAwayListener] Fix mounting behavior in Portals in React 17#23315
Merged
oliviertassinari merged 4 commits intoNov 1, 2020
Conversation
eps1lon
marked this pull request as ready for review
October 30, 2020 13:30
oliviertassinari
approved these changes
Oct 30, 2020
Member
Author
|
Should have a test for it now. |
eps1lon
force-pushed
the
fix/ClickAwayListener/react-17
branch
from
October 31, 2020 10:43
802f929 to
6638a8e
Compare
eps1lon
marked this pull request as draft
October 31, 2020 10:43
eps1lon
force-pushed
the
fix/ClickAwayListener/react-17
branch
from
October 31, 2020 10:57
6638a8e to
d52e2b4
Compare
eps1lon
marked this pull request as ready for review
October 31, 2020 12:27
oliviertassinari
approved these changes
Oct 31, 2020
Member
|
@eps1lon Great job! |
This was referenced Nov 2, 2020
2 tasks
2 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
We actually rely on the
useEffectsemantics i.e. the click handler should not be attached synchronously. Otherwise, we might handle the event that mounted the component. Unfortunately, react 17 has a bug where effects are flushed synchronously in portals so we have to find a userland workaround.Benefits over #23313: Since we have many listeners we would need to repeat it whenever we add the event listeners. Touches might behave the same as clicks so we're fixing the same problem for touch events as well. There's an argument to be made to get rid of
mountedRef/activatedRefentirely since that's no different from just removing the handler but the "mounted"-pattern helps here since we don't have to repeat this quirky logic everywhere. If the bug is fixed we should be able to get rid of the ref entirely. That IE11 comments is curious. React 17 removes the listener asynchronously anyway so I wonder if there's a realistic chance that we trigger a click-away between unmount and removal of the listener.I was not able to write a test for it since the synchronous flush only happens on a user-click and not programmatic (element.click) or dispatched (element.dispatchevent) click.react/react#20074 tracks the React bug.
Closes #23215
Closes #23313
Test plan