Warning: Can't perform a React state update on an unmounted component. #29722
-
DescriptionI've encountered this warning after upgrading. Most warning says the
I have that because I used document in the like this:
so when you scroll down the nav will be top sticky bar with background color... otherwise none.
Steps to reproduceI've recently updated to gatsby 2.32.3 and found several issues... this warning is one example I haven't solved...Difficult to reproduce since I have no idea which part caused it. Expected resultNo console warning in red. Actual resultSee the warning above. Environment
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Wherever you are adding that 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. |
Beta Was this translation helpful? Give feedback.
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: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.