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

useSyncExternalStore causes Error on Hydration of SSR React Page #3669

Closed
MikeMatrix opened this issue Mar 27, 2023 · 5 comments · Fixed by #3672
Closed

useSyncExternalStore causes Error on Hydration of SSR React Page #3669

MikeMatrix opened this issue Mar 27, 2023 · 5 comments · Fixed by #3672
Labels

Comments

@MikeMatrix
Copy link

Intended outcome:
MobX shouldn't throw an error and function as expected when part of a SSR stack. It should provide a getServerSnapshot parameter to the useSyncExternalStore call, which is required for hydrating pages.

Actual outcome:
Currently after the latest update (which to my knowledge moved MobX' React Integration to support useSyncExternalStore hydration of pages using MobX will cause

react-dom.development.js?ac89:16784 Uncaught Error: Missing getServerSnapshot, which is required for server-rendered content. Will revert to client rendering.
    at mountSyncExternalStore (react-dom.development.js?ac89:16784:1)
    at Object.useSyncExternalStore (react-dom.development.js?ac89:17727:1)
    at useSyncExternalStore (react.development.js?1b7e:1676:1)
    at useObserver (useObserver.js?cb83:74:25)
    at eval (observer.js?b6e3:42:27)
    at renderWithHooks (react-dom.development.js?ac89:16305:1)
    at updateFunctionComponent (react-dom.development.js?ac89:19588:1)
    at updateSimpleMemoComponent (react-dom.development.js?ac89:19425:1)
    at updateMemoComponent (react-dom.development.js?ac89:19284:1)
    at beginWork (react-dom.development.js?ac89:21673:1)
    at HTMLUnknownElement.callCallback (react-dom.development.js?ac89:4164:1)
    at Object.invokeGuardedCallbackDev (react-dom.development.js?ac89:4213:1)
    at invokeGuardedCallback (react-dom.development.js?ac89:4277:1)
    at beginWork$1 (react-dom.development.js?ac89:27451:1)
    at performUnitOfWork (react-dom.development.js?ac89:26557:1)
    at workLoopSync (react-dom.development.js?ac89:26466:1)
    at renderRootSync (react-dom.development.js?ac89:26434:1)
    at performConcurrentWorkOnRoot (react-dom.development.js?ac89:25738:1)
    at workLoop (scheduler.development.js?bcd2:266:1)
    at flushWork (scheduler.development.js?bcd2:239:1)
    at MessagePort.performWorkUntilDeadline (scheduler.development.js?bcd2:533:1)

How to reproduce the issue:
I have a fairly complex setup and sadly not enough time to provide a reproduction of the effect.
It occurs for me when I utilize the observer(Component) wrapper function, from what I can see.

Versions
4.0.2

@urugator
Copy link
Collaborator

urugator commented Mar 27, 2023

I have a PR ready (#3670), but to be clear, does it throw on the server or during hydration on the client? React documentation is perhaps a bit misleading?

If you omit this argument, rendering the component on the server will throw an error.

Actually throws on the client? ...

If there is no meaningful initial value for the server rendering, omit this argument to force rendering on the client.

But then it throws ? ...

@MikeMatrix
Copy link
Author

From what I checked, yes it throws.
To give some additional context, this is running on a nextjs setup, however the error seems to be thrown here:

https://github.com/facebook/react/blob/afea1d0c536e0336735b0ea5c74f635527b65785/packages/react-reconciler/src/ReactFiberHooks.js#L1592

I appreciate your effort, I might have the chance to check your PR out if it would fix my problem tomorrow.

@cmdcolin
Copy link

I had the same issue and tested out the PR at #3670 and looks like it fixed it for me. my setup is fairly weird...a web worker generates some html and we use it to hydrate stuff on the main thread

I added a console.log right before the throw to determine if it's on the server side (which in my case is a web worker) or main thread, and it is main thread

image

@cmdcolin
Copy link

cmdcolin commented Mar 28, 2023

might be able to see reproduction here https://codesandbox.io/s/great-carlos-l6ewrk?file=/App.js (see problems tab)

@github-actions github-actions bot mentioned this issue Mar 28, 2023
@MikeMatrix
Copy link
Author

Just finished testing and based on 58bb052 it seems to work properly now.
Thank you very much for the fast Fix! 👍

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

Successfully merging a pull request may close this issue.

3 participants