preactjs / signals

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

Out-of-order effect #457

Closed dolie closed 7 months ago

dolie commented 7 months ago

Hello 👋,

I got this error, may you help me understand where the problem is ? 🙏

This error occurs when I click a button to trigger some async calls. I don't see any cyclic dependencies... And don't really understand the error, and why it occurs...

Error & Callstack :

Capture d’écran 2023-11-29 à 17 58 48

Files associated with callstack

Capture d’écran 2023-11-29 à 17 50 04 Capture d’écran 2023-11-29 à 18 00 04 Capture d’écran 2023-11-29 à 17 57 08 Capture d’écran 2023-11-29 à 18 00 38
XantreDev commented 7 months ago

In which environment are you using signals? Is it react or preact? Which bundler are u using? Vite? Webpack? If it's react and vite - you can try out my react integration - I've tested it for this case (it happens while component recursive rerender itself)

rschristian commented 7 months ago

You need to provide an actual minimal reproduction, poorly cropped screenshots are far from adequate for helping us to help you.

dolie commented 7 months ago

Hello @XantreGodlike, Thank you for your fast reply. 🚀 I resolved the error.

For posterity :

Env : Preact & Vite & SWR

I suspected that useSignalEffects were re-triggered by the _mutate. So, I disabled all useSignalEffect (effects A,B & C) of page's components. Effect C was so small and did not have much interactions with the two others. So I reactivate it. Keeps running ✅ A and B were both watching the mutated signal. I decided to merge those into one useSignalEffect, and it corrected the all thing. ✅

useSignalEffect are powerful, but errors are difficult to track down... there were no clues between _mutate and those useSignalEffects... also "Out-of-order effect" is not really meaningful.

Also, if you know an eslint plugin that report cyclic dependencies in useSignalEffect, I am really interested. 🤓

Thank you again for your fast reply and for your work 🔥.