Next.js
Jotai has support for hydration of atoms with useHydrateAtoms
. The documentation for the hook can be seen here.
You can't return promises in server side rendering
It's important to note that you can't return promises with SSR - However, it's possible to guard against it inside the atom definition.
If possible use useHydrateAtoms
to hydrate values from the server.
const postData = atom((get) => {const id = get(postId)if (isSSR || prefetchedPostData[id]) {return prefetchedPostData[id] || EMPTY_POST_DATA}return fetchData(id) // returns a promise})
Examples
Clock
HN Posts
Next.js repo
npx create-next-app --example with-jotai with-jotai-app# oryarn create next-app --example with-jotai with-jotai-app
Here's a link.