Some questions about the provider #2619
-
When using react-router-dom, multiple routes correspond to multiple Providers. If a key is not added, it will lead to abnormal state behavior, meaning the state will not be cleaned up when the page is unmounted. By adding a key, the state of the Provider will automatically reset when the page is unmounted. eg: <BrowserRouter>
<Route><Provider>{pageChildren}</Provider><Route>
<Route><Provider>{pageChildren}</Provider><Route>
<Route><Provider>{pageChildren}</Provider><Route>
<BrowserRouter> to <BrowserRouter>
<Route><Provider key={route.path}>{pageChildren}</Provider><Route>
<Route><Provider key={route.path}>{pageChildren}</Provider><Route>
<Route><Provider key={route.path}>{pageChildren}</Provider><Route>
<BrowserRouter> |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 9 replies
-
I just have some doubts and haven't thoroughly investigated. |
Beta Was this translation helpful? Give feedback.
-
Code SanBox Demo I'm not sure if this is an issue or if there's a problem with my usage. I would like to ask for your help. |
Beta Was this translation helpful? Give feedback.
-
I can also encapsulate a Provider with a key as a common component for reuse. import { useRef } from 'react';
import { Provider } from 'jotai' ;
import { nanoId } from 'nanoid';
export default function WrapperProvider({ children, ...props }) {
const scopeKey = useRef(nanoId(7)).current;
return <Provider {...props} key={scopeKey}>{children}<Provider>
} |
Beta Was this translation helpful? Give feedback.
-
Also check out https://github.com/jotaijs/jotai-scope which may or may not be related. |
Beta Was this translation helpful? Give feedback.
Adding key seems the right solution.