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

React has detected a change in the order of Hooks when Lazy loaded #414

Open
pavelrotek opened this issue Sep 19, 2023 · 14 comments
Open

React has detected a change in the order of Hooks when Lazy loaded #414

pavelrotek opened this issue Sep 19, 2023 · 14 comments
Labels

Comments

@pavelrotek
Copy link

pavelrotek commented Sep 19, 2023

Hi everyone,

when using import @preact/signals-react in lazy loaded component (Suspense), and react tree rerenders, the following error is thrown. It relates probably to preact signal render optimizations (when using react signal "lite" mentioned in issue (#251 (comment) everything seems to work correctly).

Warning: React has detected a change in the order of Hooks called by Lazy. This will lead to bugs and errors if not fixed. For more information, read the Rules of Hooks: https://reactjs.org/link/rules-of-hooks

Previous render Next render

  1. useRef useMemo
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Can be reproduced on my public repo... https://github.com/pavelrotek/signals-error

screens attached
before rerender before rerender

after pressing "refresh" button
after rerender

@oliviercperrier
Copy link

oliviercperrier commented Nov 1, 2023

Got the same thing in expo web app using expo-router v2.

Everything is working with version 1.2.1 of @preact/signals-react tho. Not sure what has changed.

Screenshot 2023-11-01 at 7 24 29 PM

@rgrenonlkr
Copy link

We experienced the same issue and ended up import our signals (not using them) in a not-lazy component.
Don't know why but after that we can use signals in our lazy loaded components.

@tonygustafsson
Copy link

Pulled my hair over this one... read this comment about lazy loaded component and didn't believe it. :P But it's true... making it an ordinary componet worked. However, I would love to understand why this happens.

@XantreDev
Copy link
Contributor

Current preact signals to react integration alter ReactCurrentDispatcher behaviour to track signals in each component. It is quite a complex task (because no one knows how react will behave in some scenarios). There are tests, but probably this case is not covered

@kaisergeX
Copy link

We encountered a similar issue with react-router-dom when navigating between routes after declaring a signal.
If I use a regular component instead of the lazy one for routes, these errors disappear.

image

Reproduce issue: https://codesandbox.io/p/sandbox/preact-signals-react-and-react-router-dom-68g3dk

@panovpassendo
Copy link

We encountered the same issue with our app after declaring a signal and using version 1.3.6
"Warning: React has detected a change in the order of hooks..."
After setting it to an earlier version, 1.2.1, no issues.

@XantreDev
Copy link
Contributor

In which envirement do you using signals?

@XantreDev
Copy link
Contributor

I've tried to fix this kind of problems in my react integration of signals. You can try it out with vite now

@oyee91
Copy link

oyee91 commented Nov 22, 2023

We encountered a similar issue with react-router-dom when navigating between routes after declaring a signal. If I use a regular component instead of the lazy one for routes, these errors disappear.

image

Reproduce issue: https://codesandbox.io/p/sandbox/preact-signals-react-and-react-router-dom-68g3dk

This is a major issue for us as well, is this issue being looked at or not, there is a perfect and straight forward way of reproducing this.

@XantreDev
Copy link
Contributor

@oyee91 you can try to use my signals integration as well. If you will have any issues I will try to fix it asap. I actually can try to provide a PR that tries to fix it, but it will take some time for research + review

@sahajjain1
Copy link

sahajjain1 commented Nov 28, 2023

@XantreGodlike getting the same error : React has detected a change in the order of hooks... while using @preact/signals-react@1.1.1 and cant update the signal version above 1.1.1 as it will not work with my React version 17.0.2
any suggestions?
image

@PHILIPP111007
Copy link

I imported "@preact/signals-react" in index.js file

@devcrev
Copy link

devcrev commented Nov 30, 2023

@PHILIPP111007's solution worked for me.

@XantreDev
Copy link
Contributor

@PHILIPP111007 yep it can fix this issue, because if we are importing signals async - react will not to be patches initially and after patch order of hooks will change. Probably this info should be in README. @rschristian Should I provide PR?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests