-
Notifications
You must be signed in to change notification settings - Fork 47.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Bug: [email protected]
event delegation issue
#20636
Comments
I don't think there's a bug here. You can reproduce the same behavior with vanilla DOM: https://codesandbox.io/s/immutable-wildflower-w82nx?file=/src/index.js document.getElementById("app").innerHTML = `
<button id="btn">
click me
</button>
`;
document.getElementById("btn").onclick = function () {
document.addEventListener("click", function () {
alert("document click fired");
});
}; If you add a document listener during a button click, then that listener will fire (as a part of that click propagating upwards). This didn't happen in React <17 because React used to register its events at the document level. So React handlers would run too late (when the event has already bubbled to the document level), and thus registering another document-level listener would not have a consequence. But React 17 behaves closer to how regular DOM works. |
Have you considered using something like https://reach.tech/dialog instead of rolling your own? |
Hi @gaearon and thanks for taking your time to help me figure this out. Yes, i saw in the release notes that the listener was moved to the app root rather than keeping it at the document level, so i would expect things to bubble (and i know i can work around this by stopping propagation), but the weird thing to me is that, in my head, the effect in To me it looks like:
Maybe there is something asynchronous going on which i am missing. I'm using the Chrome debugger and seeing that the handler of my To continue on your vanilla example: (run here: https://codesandbox.io/s/falling-tdd-0tthx?file=/src/index.tsx):
If we add a Regarding the library i don't think i need it in my case (i'm using a small little EDIT: Of course, this is all based on that |
Here is a fork of the original codesandbox without passing https://codesandbox.io/s/react-table-react-17-forked-212ou?file=/src/App.js |
Was there any changes to how fiber renders in 17.x? Maybe it all boils down to synchronous vs asynchronous rendering after |
okay thanks
…On Mon, Jan 25, 2021 at 11:29 AM Oscar Linde ***@***.***> wrote:
Was there any changes to how fiber renders in 17.x?
Maybe it all boils down to synchronous vs asynchronous rendering after
setOpen - if in React 16 it was async, but in 17.x it is optimized to be
sync then that would explain the difference in behavior. I can't find any
release notes about this, so i'm not sure whether this side effect is
something that was considered or if it is actually some unintended side
effect.
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
<#20636 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/APYPGPVLSTRTWRTURPQVYVDS3VI2NANCNFSM4WNIAS2A>
.
|
We faced similar issue(s) after migrating to [email protected]. |
Related: #20863 (comment) |
React 17 changes the root of event delegation target from imo the event bubbling here in React Component has some inconsistency to DOM implementation, it should be noted when user tries to do delicate event bubbling manipulation. |
This issue has been automatically marked as stale. If this issue is still affecting you, please leave any comment (for example, "bump"), and we'll keep it open. We are sorry that we haven't been able to prioritize it yet. If you have any new additional information, please include it with your comment! |
Closing this issue after a prolonged period of inactivity. If this issue is still present in the latest release, please create a new issue with up-to-date information. Thank you! |
This is pretty much a duplicate of TanStack/table#3002
React version: 17.x
Bug description
As i was developing i forgot to memoize some mocked data before passing it to react-table's
useTable
hook and encountered an interesting bug, which led me down a rabbit hole instead of actually seeing the problem (missed useMemo on my data)It only happens when you use
useTable
combined withuseSortBy
, and only with[email protected]
(event delegation rewrite release), e.g[email protected]
works fine.It is hard to put to words, but i have attached a minimal example (codesandbox) and will try my best to explain. In the codesandbox i have three components:
Modal
which has auseEffect
that adds a click event listener todocument
. This listener acts like a backdrop (where if i click the backdrop the "modal" should close. Rendered by App.Table
which is the component that callsuseTable
(but doesn't use anything it returns) - also rendered by App.App
which is the main application and controls the state that decides whetherModal
is rendered or not. Here i actively "forgot" to useuseMemo
on my data.What happens is that when you click "Open Modal" using
[email protected]
the callback todocument.addEventListener('click', ..)
is called, but this element shouldn't have been rendered yet at the point when the click event from the button was fired. The result is that you never, or very briefly, see the "modal". Doing the same using[email protected]
(you can change in the sandbox) results in the expected behavior of the "modal" actually showing.Codesandbox example
https://codesandbox.io/s/react-table-react-17-5nw4d?file=/src/App.js
Steps To Reproduce
Steps to reproduce the behavior:
react-dom
version is17.0.1
react-dom
to 16.14.0 for example.Expected behavior
Obviously i can't expect the react-table library to work properly, but i would not expect it to have the weird observed undefined behavior. I also would not expect this to have different behaviors across
react-dom
versions, even if it is a major release.Additional context
I am very curious to what is actually going on and whether this is a bug of
react-dom
orreact-table
or a combination of both. I've been debugging to try and find the root cause, but unfortunately i'm not very familiar with the code of either repos and i believe there are things happening asynchronously which is very hard for me to follow.It would be nice if anyone wanted to help me debug this issue, so we can get to the root of the problem and perhaps have a more minimal example.
The text was updated successfully, but these errors were encountered: