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: strict mode and missing context value #234

Closed
wants to merge 1 commit into from

Conversation

raRaRa
Copy link

@raRaRa raRaRa commented Nov 19, 2023

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.

Copy link

vercel bot commented Nov 19, 2023

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
react-scroll-parallax ✅ Ready (Inspect) Visit Preview 💬 Add feedback Nov 19, 2023 8:04pm

@brendenmoore
Copy link

Can confirm this fixed it for me in NextJS 14

@jscottsmith
Copy link
Owner

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.

jscottsmith added a commit that referenced this pull request Nov 21, 2023
- related to strict mode issues #233 #227 and #221
- credit to @raRaRa for #234
@jscottsmith
Copy link
Owner

Published v3.4.4

@raRaRa
Copy link
Author

raRaRa commented Nov 21, 2023

Closing this PR because the destroy was required. Ref: #235

@raRaRa raRaRa closed this Nov 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants