-
Notifications
You must be signed in to change notification settings - Fork 47.3k
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: createPortal anywhere in the tree makes native events be ran too late #21989
Labels
Comments
kosciolek
added
the
Status: Unconfirmed
A potential issue that we haven't yet confirmed as a bug
label
Jul 29, 2021
Turns out, strictly a portal is not needed. Adding this to the described component causes the same bug (?)
|
However, changing it to Complete component code: function App() {
const [popupOpen, setPopupOpen] = useState(false);
useEffect(() => {
console.log('binding', popupOpen);
const listener = () => {
console.log("NATIVE", popupOpen);
if (popupOpen) setPopupOpen(false);
};
window.addEventListener("click", listener);
return () => window.removeEventListener("click", listener);
}, [popupOpen]);
const [state, setState] = useState<HTMLDivElement | null>(null);
return (
<>
<div
onClick={() => {
console.log("REACT", popupOpen); // REACT HANDLER STATE
if (!popupOpen) setPopupOpen(true);
}}
>
Open popup
</div>
<div ref={setState}></div>
</>
);
} |
Thanks for the report. Could you check if this is similar to #20074? |
Closing then as per #20074 (comment) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Labels
Summary
Native events added via
useEffect
are called too late (and with improper (new) state, rather than the state during listener attachment) if there's aReactDOM.createPortal
anywhere in the tree. First the effect is re-run and a new listener is attached, and only then the native event is called.React version:
17.0.2
Link to code example:
CodeSandbox
Reproduction
createPortal
. It can even becreatePortal(null, document.body)
useState
.onClick
handler passed.useEffect
is used to attach aclick
event towindow
div
) usesetState
.The current behavior
The expected behavior
In other words
Logs
But without
createPortal
, the following is logged:The text was updated successfully, but these errors were encountered: