useHydrateAtoms causes hydration mismatch, unlike React context
#3052
-
Bug DescriptionI’ve got an app where I read a This setup works fine on the first render, but if I change the cookie and refresh the page, I get a hydration mismatch error. Interestingly, if I turn on What’s more confusing is that I tried this exact flow with React context instead of Jotai, and everything worked perfectly — no hydration issues at all. So I’m wondering: Steps to reproduce:
Repeat the same steps on the "With dangerously" link and you’ll see both context and Jotai behave the same. Here's a screen recording of both flows:
without-dangerously-flag.mp4
with-dangerously-flag.mp4So yeah — just trying to understand the difference here. I assumed Jotai and context would behave similarly in SSR scenarios. And also, if Maybe I’m missing something — if you have any docs or resources that explain what’s going on under the hood, that'd be great. Thanks. Reproduction Link |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 5 replies
-
|
This looks wrong. export const JotaiProvider = ({
children,
initialValue,
dangerouslyForceHydrate,
}: {
children: React.ReactNode;
initialValue: string;
dangerouslyForceHydrate?: boolean;
}) => {
useHydrateAtoms([[currencyAtom, initialValue]], {
store,
dangerouslyForceHydrate,
});
return <Provider store={store}>{children}</Provider>; |
Beta Was this translation helpful? Give feedback.
This looks wrong.
useHydrateAtomsshould be used under the<Provider>tree.And you shouldn't pass
store={store}prop, otherwise it can be shared across sessions.