-
Notifications
You must be signed in to change notification settings - Fork 45.6k
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
Bug: Context provider updates forcing Suspense fallback on hydration #24476
Comments
I described exact same problem in this topic: My project uses Next.js and React Query. Can you post a code snippet how exactly you solved it with useMemo? React Query has it's own cache and I still get this error. |
This is intentional behavior: if a tree hasn't finished hydrating yet, and it receives an urgent update via either props or context, React will give up hydrating and render the update instead. This is considered a "recoverable error" because the app doesn't crash, it just discards the HTML and reverts to client-only rendering. But this can have performance implications, so we log it anyway so the developer knows it needs to be fixed. There are a few ways you can fix it, depending on the use case:
Does this make sense? We're working on ways to make the developer experience better, like including the name of the component that caused the update. Also, if you could share more details on what you were trying to achieve when you hit this issue that could be helpful for us when communicating about this in the future. |
This is very difficult error to resolve, it occurs so randomly. I memoized children, it occurs less frequently but still happens sometimes on page refresh. I don't have access to original state update, it happens in useQuery hook from React Query. |
Yeah memoizing the children doesn't work if a prop actually has changed. If the update is coming from somewhere in React Query you may need to ask them if they can provide a way to wrap data updates in |
Btw we agree with your feedback that it's a difficult issue to debug. We have some ideas for how to improve the developer experience, like showing the name of the component or hook that triggered the update, or integrating with React DevTools. |
@nemanjam I'll take your thumbs up to indicate that this can be closed, let me know if that's wrong. |
@acdlite Whilst we wait for the developer experience to be improved, do you have any tips to help debug these error messages?
At Unsplash we're trying to migrate from |
I think the stack trace ends with the setState call triggering it. Is that not the case? |
Hmm I see. Until we have something better, some ways you can try debugging it:
|
I'm running into a very similar issue when using |
I'm facing a similar issue with Suspense and useSyncExternalStore. vercel/next.js#43920 The original issue was that I couldn't use zustand store, which uses useSyncExternalStore under the hood, inside a component wrapped in Suspense. Copying here a stackblitz to reproduce: https://stackblitz.com/edit/nextjs-yuqb6g?file=app%2FPageComponents.js In the example, I provide a |
React version: 18.1.0
Steps To Reproduce
Link to code example:
https://codesandbox.io/s/react-18-redux-ssr-forked-rbkvtv?file=/src/App.js (forked from a redux ssr sandbox bc I originally thought this was a hydration issue with redux)
The current behavior
The update to the context provider causes the suspense boundary to switch to the fallback since it has not finished hydrating yet and shows this error:
The expected behavior
The suspense boundary is able to hydrate without switching to the fallback on the client.
Note
Updating the ContextProvider to memoize the value using
useMemo
addresses the issue but I am still curious if this is expected behavior or a bug.The text was updated successfully, but these errors were encountered: