Skip to content
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

Fix regression in attaching event handlers #368

Closed
wants to merge 2 commits into from

Conversation

awalker-stripe
Copy link
Contributor

Summary & motivation

Fix for #365

In some integrations, the useEffect that calls elements.on seemed run while elementRef.current was still null, resulting in the event handler never being attached. This (hopefully) fixes the issue by explicitly attaching the event handler any time we set elementRef.current.

Testing & documentation

}, [cb, event, elementRef]);

React.useLayoutEffect(() => addEventCallback(), [addEventCallback]);
Copy link
Contributor Author

@awalker-stripe awalker-stripe Jan 3, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

useLayoutEffect instead of useEffect makes sure we only call element.on once when first rendering. This is because this layout effect will execute before the layout effect that creates the element, meaning when rendering, elementRef.current is null and it'll exit early on line 16. (Please lmk if there's a better way around this!)

@awalker-stripe
Copy link
Contributor Author

r? @jackieosborn-stripe

@awalker-stripe awalker-stripe self-assigned this Jan 4, 2023
@awalker-stripe awalker-stripe deleted the awalker/fix-events branch January 10, 2023 21:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant