Replies: 6 comments 16 replies
-
Basically, the route change is not hydration. Hydration is only for the initial render. So, we need to update with both useHydrateAtoms and useEffect for the route change. ...if I'm not mistaken. The idea is this: const useSyncAtom = (atom, value) => {
useHydrateAtoms([atom, value]);
const setAtom = useSetAtom(atom);
useEffect(() => {
setAtom(value);
}, [setAtom, value]);
}; |
Beta Was this translation helpful? Give feedback.
-
Well, when i change navigation to a new Page (wich is a Server Components) i fetch some data and return them as a prop to the Page. So a new state is computed server side and I think it is an hydration. Right now I'm looking this |
Beta Was this translation helpful? Give feedback.
-
Furthermore, if I use Detected multiple Jotai instances. It may cause unexpected behavior with the default store. #2044 |
Beta Was this translation helpful? Give feedback.
-
@ootkin it's a feature of the Let's say you have a page like This might be confusing, as you are correct - the SSR takes place, so we expect hydration... but since the This means, that if you have So the solution is to use the Other (hypothetical) solution could be to call const {evict} = useHydrateAtoms([[atom, value]]); This could help for other client side navigation cases like evict() // enables subsequent hydration
router.refresh() // fetches new data & merges the RSC
// form will be reinitialized With such cache eviction we won't need to set the atoms manually after the hydration useEffect(() => {
setAtom(value);
}, [setAtom, value]); This to me looks like a "second hydration" and can be confusing, or easy to forget. @dai-shi thoughts? |
Beta Was this translation helpful? Give feedback.
-
related: #669 |
Beta Was this translation helpful? Give feedback.
-
@ootkin I believe you can use the root template for the app router in Nextjs. It should re-create the I did some rudimentary testing with this and it seemed to fix the issue for me. // app/template.tsx
import {Provider} from 'jotai'
interface RootTemplateProps {
children: React.ReactNode
}
const RootTemplate: React.FC<RootTemplateProps> = ({children}) => (
<Provider>
{children}
</Provider>
)
export default RootTemplate If you want to synchronize the hydration with cache invalidation (e.g. |
Beta Was this translation helpful? Give feedback.
-
Hi,
I would like to integrate Jotai in a next.js (v14) using app routers.
Following the Jotai documentation (even if the example reports page routers instead) I see that we need to use a global Provider.
I tried to implement the Provider in the RootLayout (
/app/layout.tsx
) but it seems like to work only when i refresh the page: when i change page usingrouter.push()
the state is not hydrated withuseHydrateAtoms
.Do you have any ideas? Maybe the Provider must be placed in each page and in the root layout.
As the Next.js documentation says, the layout persist across routes and maintain state.
Thanks
Beta Was this translation helpful? Give feedback.
All reactions