-
Notifications
You must be signed in to change notification settings - Fork 156
fix: strict mode and missing context value #234
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
Conversation
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
brendenmoore
commented
Nov 20, 2023
Can confirm this fixed it for me in NextJS 14
Thanks for digging into this -- was helpful to see and definitely related to strict mode, but can't replace the call to destroy
with disableParallaxController
since that only disables effects.
I believe the problem was mainly around how the useRef
was used, it's was creating a new controller each render, and setting it to null
in the cleanup as you said.
I tested this on Next 14, and it seems to fix it.
I'll publish it later this evening.
Published v3.4.4
Closing this PR because the destroy was required. Ref: #235
Uh oh!
There was an error while loading. Please reload this page.
This PR fixes #233 #227 and #221
The issue was that the controller could become null in between renders, e.g. when React is in strict mode. Strict mode in React makes all useEffects run twice, to make sure they don't have any memory leaks, etc. More can be read here: https://legacy.reactjs.org/docs/strict-mode.html#ensuring-reusable-state
What happened is that the last useEffect that handles unmount got called twice, thus the controller became null. This is not a NextJS issue, but React Strict mode issue (strict mode with useEffect came in React 18). Because of this, the useEffect should always handle both mount & unmount (never assume that unmount gets called once).
With this PR I never make the controller null, instead I just disable the controller which should clean all the event listeners. Please correct me if I'm wrong.
I've tested these changes in NextJS app router and it seems to fix all of the issues.