Skip to content
Discussion options

You must be logged in to vote

Wherever you are adding that document.addEventListener you need to remove it when the component is unmounting to prevent it being called afterwards (which is why it's throwing that error). For example:

useEffect(() => {
document.addEventListener(() => { //things that setState })
return () => { document.removeEventListener() }
}, [])

That way React will ensure it is cleaned up correctly, it's also more idiomatic that side effect + clean up is contained together, rather than split across components.

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@AnnieTaylorCHEN
Comment options

Answer selected by AnnieTaylorCHEN
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Help
Labels
None yet
2 participants
Converted from issue

This discussion was converted from issue #29719 on February 24, 2021 07:44.