-
-
Notifications
You must be signed in to change notification settings - Fork 567
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
Next.JS example is not wrapped with a Provider - is it intended? #1272
Comments
Yeah, it's better to have it for SSR. It's why jotai is Context based. |
I'll update the example 👍🏼 |
Alright, thanks for the clarification! By the way. documentation examples might need an update as well 😄 |
No, |
So, what is the "recommended" way then? |
Yeah, actually the more I try to understand the more confused I am 😄 I have noticed we can provide If it's possible, then I wonder if passing |
I'd say that wrapping your app in a Lets say you have a import { atom, useAtom } from 'jotai'
import { useHydrateAtoms } from 'jotai/utils'
const countAtom = atom(0)
const CounterPage = ({ countFromServer }) => {
useHydrateAtoms([[countAtom, countFromServer]])
const [count] = useAtom(countAtom)
// count would be the value of `countFromServer`, not 0.
} This has the following benefits:
I hope this is helpful @MihailPertsev and @TenkoSpirit. |
Ok, so we still need to wrap our Is this one correct? const MyApp = function MyApp({ Component, pageProps }) {
const [queryClient] = useState(() => new QueryClient());
return (
<QueryClientProvider client={queryClient}>
<Hydrate state={pageProps.dehydratedState}>
<JotaiProvider>
<Component {...pageProps} />
</JotaiProvider>
</Hydrate>
<ReactQueryDevtools initialIsOpen={false} />
</QueryClientProvider>
);
}; Or that one: const MyApp = function MyApp({ Component, pageProps }) {
const [queryClient] = useState(() => new QueryClient());
return (
<JotaiProvider>
<QueryClientProvider client={queryClient}>
<Hydrate state={pageProps.dehydratedState}>
<Component {...pageProps} />
</Hydrate>
<ReactQueryDevtools initialIsOpen={false} />
</QueryClientProvider>
</JotaiProvider>
);
};
|
The first one. Then you can use the common |
Alright, so basically the hydration hook isn't exactly required but can be very useful. I actually have a good use case for this hook in my project then! Thanks a lot for the explanation and your time ❤️ |
No problem! Closing this :) |
Hello! I was switching from Vue and had a need for some state management, so I tried Jotai and really enjoy it so far ❤️
The only thing that is a little bit unclear for me is the use of
Provider
. I have read the discussion in #1207 and related issues, and it seems likeProvider
is extremely necessarily for SSR / SSG (correct me if I am wrong about it actually).So I thought if the
Provider
wrapper component that important, then maybe NextJS example should be updated as well? The example's app isn't wrapped withProvider
and it's index page seems to be using SSR, which is a little bit confusing then 😅The text was updated successfully, but these errors were encountered: