-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
[mobx-react] Maximum update depth exceeded
error since mobx-react@8
#3865
Comments
Eh... that test description definitely explains issues I've seen around automation with other projects in the past, even not MobX related 😅 curious if #3863 has an effect on this |
If you can release it for preview I can give it a shot! |
Hi @mweststrate my PR won't have any effect on this as the PR is for class component only.
The problem here is cc @jeffijoe |
@zhantx for context, when using regular React state (without mobx) in the Downshift reproduction, it only runs twice instead of looping, that's why I opened an issue here as well. |
That's right, let me explain for regular React state
when using mobx, because mobx is an external store, naturally, it uses
It seems to me that mobx-react is doing what we expect, that if any store mutates after render phase, we should let react to re-render the component. and because mobx is an external store, any updates should be in SyncLane |
Intended outcome:
There should be no
Maximum update depth exceeded
errorActual outcome:
React throws a
Maximum update depth exceeded
error.How to reproduce the issue:
I created a reproduction repository (see
src/MobX.jsx
) - it includes bothmobx
anddownshift
, this is becausedownshift
is the root cause however the waymobx-react
behaves after version 8 (with the change touseSyncExternalStore
) makes me believe it's worth investigating on themobx-react
side.You'll be able to see for yourself by downgrading to
mobx-react@7
in that repo and running the test - it will show2
in the counter instead of53
(at which point the error is thrown which stops the loop).I know the issue template states to avoid speculation but I've spent way too long investigating what's going on to not not mention it 😅 What seems to be happening is the
useSyncExternalStore
will "push" another work item on the internal React sync queue (for batching) and because every reaction will makemobx-react
's implementation return a new symbol ingetSnapshot
, if React triggers an effect which subsequently triggers a MobX reaction, it will keep pushing to that queue for each iteration.mobx/packages/mobx-react-lite/src/useObserver.ts
Lines 49 to 72 in 13a74fe
New work pushed to queue
As I mentioned, the root cause is a different library, but what prompted me to look into this was because in my real application, the browser tap freezes when this happens as the error actually loops infinitely there. I couldn't reproduce that here though. Additionally, when using plain React
useState
(as seen inBasic.jsx
), it does not result in the error.I have also opened an issue with
downshift
.Versions
[email protected]
[email protected]
[email protected]
[email protected]
The text was updated successfully, but these errors were encountered: