Detected multiple Jotai instances. It may cause unexpected behavior with the default store. #2072
-
I suddenly am encountering this error after upgrading to Next.js v13.4.13 from the previous version. My root layout.tsx file includes a provider for Jotai, thats the only place in the application the provider can be found. Here is the code of the provider component: "use client";
import { Provider } from "jotai";
import store from "global/store";
interface Props extends React.PropsWithChildren {}
export default function JotaiProvider({ children }: Props): JSX.Element {
return <Provider store={store}>{children}</Provider>;
}
export type { Props as JotaiProviderProps }; Here I am simply passing the store created in import { getDefaultStore } from "jotai";
const store = getDefaultStore();
export default store; Am I doing something wrong here? This error may be related to the fix #2066, but again that is just a wild guess. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 20 replies
-
That's exactly the case the warning is trying to detect. If you are using Nextjs, it's recommended to avoid using the default store. - return <Provider store={store}>{children}</Provider>;
+ return <Provider>{children}</Provider>; |
Beta Was this translation helpful? Give feedback.
-
It depends on your requirement, but in general, SSR framework like Next.js shares memory across multiple sessions and you want to make sure to use different store for each session. Removing |
Beta Was this translation helpful? Give feedback.
That's exactly the case the warning is trying to detect.
Using getDefaultStore() assumes single Jotai instance.
If you are using Nextjs, it's recommended to avoid using the default store.