-
Notifications
You must be signed in to change notification settings - Fork 47.2k
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: useEffect Timing changes depending on if Portal was rendered #20074
Comments
17 only? |
17 only! downstream issue react-bootstrap/react-bootstrap#5409 |
well tbh maybe this was present but not observable since before 17 you couldn't attach an event listener "higher up" so there wasn't any way to get in front of the current event |
What sounds like a bug here is that |
I'm sooo confused. I can't reproduce this anymore. The only thing I did was to update Chrome... |
Can you still repro it? |
bah sorry @gaearon i was goofing around trying to find a workaround and didn't realize it was saving instead of forking. It should be back to being reproducible |
Ok at least I can still sleep at night |
What happens here is that the native event bubbles:
This is why we get into dispatchEvent twice. Root container pass schedules an effect, and body pass flushes it. Seems like a bug that we flush the effect in this case. |
To follow up on this — it does seem like a bug but we can't commit to prioritizing it at the moment. There will be a bunch of work done to clean up and simplify related parts of the code in the coming months so we'll likely address it then. Have you found any userland workaround, or is this a hard blocker for you? |
We've got a workaround that's simple and effective in theory here. Need to let folks test it out a bit though. Alternatively I think adding listeners in a For anyone looking, our current workaround is useEffect(() => {
// Note this depends on a deprecated but extremely well
// supported quirk of the web platform, this may be less effective
// for other event types, YMMV
const currentEvent = window.event
element.addEventListener('click', (e) => {
if (e === currentEvent) {
currentEvent = null;
return
}
// do the work
})
// ...clean up
}) |
What I noticed is that for an Edit: Edit2: Edit3: Edit4: Edit5: react/packages/react-reconciler/src/ReactFiberWorkLoop.old.js Lines 1061 to 1064 in 3fbd47b
react/packages/react-reconciler/src/ReactFiberWorkLoop.old.js Lines 1083 to 1085 in 3fbd47b
|
@gaearon It seems to me that the assertions in react/packages/react-reconciler/src/__tests__/ReactHooksWithNoopRenderer-test.js Lines 1705 to 1722 in c59c3df
The update that is scheduled in the passive effect is deferred as intended when flushing sync but the effect itself should not run when flushing synchronously. IMO diff --git a/packages/react-reconciler/src/__tests__/ReactHooksWithNoopRenderer-test.js b/packages/react-reconciler/src/__tests__/ReactHooksWithNoopRenderer-test.js
index 79794bbea7..6ab75eac6d 100644
--- a/packages/react-reconciler/src/__tests__/ReactHooksWithNoopRenderer-test.js
+++ b/packages/react-reconciler/src/__tests__/ReactHooksWithNoopRenderer-test.js
@@ -1706,14 +1706,16 @@ describe('ReactHooksWithNoopRenderer', () => {
ReactNoop.flushSync(() => {
_updateCount(2);
});
+
+ expect(Scheduler).toHaveYielded(['Count: 2']);
});
// As a result we, somewhat surprisingly, commit them in the opposite order.
// This should be fine because any non-discrete set of work doesn't guarantee order
// and easily could've happened slightly later too.
expect(Scheduler).toHaveYielded([
- 'Will set count to 1',
'Count: 2',
+ 'Will set count to 1',
'Count: 1',
]); would make more sense here. The test comment acknowledges that this is the opposite order and it seems to me that this issue is a good example why this order is problematic: Any side-effect that touches the DOM directly is out-of-order. |
@jquense Not sure about the intended behaviour but it seems like having the following solve your issue (at least temporarely): document.addEventListener("click", clickHandler, { capture: true }) On step 3, it displays the message correctly |
You can also fix the issue by wrapping setTimeout(() => {
document.addEventListener('click', clickHandler);
}, 1); |
Is there a fix in progress, for upcoming versions, for this bug? I am facing this issue mui/material-ui#24783 , which cant be resolved because of this. |
Could you explain in the Material-UI issue why this can't be resolved for you? We were under the impression that we worked around this bug for our specific usage. For updates on a fix, #20074 (comment) is the latest update. From what I gather the team is already experimenting with potential fixes. You can subscribe to this (#20074) github issue to get notified about updates. Asking for updates does not speed up delivery of said update. |
The team has taken a closer look at this now that the related refactors are landing. We can't fix it in a general case without relying on some heuristic. Making all effects synchronous would be bad. Making all of them asynchronous also doesn't work because sometimes we need to flush earlier. The need to flush earlier relates to handling discrete events (like click) that need to be able to observe the already flushed state. A reasonable compromise is to make them synchronous at the end of discrete events (such as click), but asynchronous in other cases. This mirrors the recent changes we've made to state updates in Concurrent Mode to make it easier to adopt. So we added this heuristic in Concurrent Mode: #21150. I checked that this issue doesn't repro in Concurrent Mode with the latest master commit and now consistently does not show the message (because clicks lead to synchronous effects). We've considered backporting this behavior to React 17. But at this point it's very risky because it's such a subtle change. It's also risky because it would be the first time that we rely on So we've concluded that, while unfortunate, it is safer to not try to fix it in React 17. But Concurrent Mode will have a more predictable behavior here. (Note that it's also quite likely that React 16 also had cases with over-flushing, but triggered under different conditions.) I know this isn't the resolution that most people were hoping for. |
Workaround already used by v0 to handle facebook/react#20074
* fix(useOnClickOutside): use conditional handler window event Workaround already used by v0 to handle facebook/react#20074 * Change files * change file * mem leak fix
Bug; Still clickAwayListener tests fails using React testing library. It seems because of react18. |
This is a weird one. Basically, if you add an event listener to the document in an effect that was triggered by an event. e.g.
click
toggles some state, which triggers an effect, which adds aclick
handler to the document. In the normal case the new event handler will "miss" the triggering event, e.g. the added click handler won't respond to the click event that triggered it being added (omg).HOWEVER, if you render a portal first, the timing changes slightly and the added event handler will see the current event.
React version: 17
Steps To Reproduce
https://codesandbox.io/s/react-playground-forked-cyt0f?file=/index.js
The reason for the final behavior is the click event both opens and closes the message, (calls set state twice)
Link to code example:
https://codesandbox.io/s/react-playground-forked-cyt0f?file=/index.js
The current behavior
The expected behavior
That they be consistent
The text was updated successfully, but these errors were encountered: