preactjs / signals

Manage state with style in every framework
https://preactjs.com/blog/introducing-signals/
MIT License
3.64k stars 89 forks source link

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

Open pavelrotek opened 10 months ago

pavelrotek commented 10 months ago

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 (https://github.com/preactjs/signals/issues/251#issuecomment-1426095471 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 commented 8 months ago

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 commented 8 months ago

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 commented 8 months ago

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 commented 8 months ago

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 commented 8 months ago

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 commented 8 months ago

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 commented 8 months ago

In which envirement do you using signals?

XantreDev commented 8 months ago

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

oyee91 commented 8 months ago

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 commented 8 months ago

@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 commented 7 months ago

@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 commented 7 months ago

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

devcrev commented 7 months ago

@PHILIPP111007's solution worked for me.

XantreDev commented 7 months ago

@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?