-
Hi, I currently have a React app that uses Context to fetch large amounts of async data in certain routes only. The problem with Context is that it updates all state and not slices of state whenever a user changes data that should only reload a part of state. Different types of users are routed to different landing pages, and I don't want to load the state for, say, BuinessCustomer when RetailCustomer logs in. I've been reading the docs and I'm still not sure how this is accomplished in Jotai. I have looked at Provider and all it seems to do is designate a separate store. Thank you! |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 5 replies
-
Thanks for opening up a discussion. Your confusion is understandable. I think it would be nice to establish a certain pattern and it in docs. To use it "contextual", I think there are some options:
I wonder if anyone else has some experience with it. |
Beta Was this translation helpful? Give feedback.
-
If all you are aiming to do it to fetch data only when you need it, then just using atoms as they are would be sufficient in my opinion. function App() {
return (
<Router>
<Route path="/home" component={Home} />
<Route path="/expensive" component={Expensive} />
</Router>
);
}
const smallDataAtom = atom(() => fetch('https://example.com/small').then((res) => res.json()));
const expensiveDataAtom = atom(() => fetch('https://example.com/small').then((res) => res.json()));
function Home() {
const smallData = useAtomValue(smallDataAtom);
return (
<Suspense fallback={"Loading..."}>
<p>Small data: ${smallData}</p>
</Suspense>
);
}
function Expensive() {
const expensiveData = useAtomValue(expensiveDataAtom);
return (
<Suspense fallback={"Loading..."}>
<p>Expensive data: ${expensiveData}</p>
</Suspense>
);
} Each |
Beta Was this translation helpful? Give feedback.
-
Ok, Thanks for the help. 🙏 |
Beta Was this translation helpful? Give feedback.
Thanks for opening up a discussion.
Your confusion is understandable. I think it would be nice to establish a certain pattern and it in docs.
So, as you know, a Jotai store is just like a WeakMap, and Provider is just to separate a store.
This means Jotai "core" is designed only for "global" store.
To use it "contextual", I think there are some options:
jotai-scope
I wonder if anyone else has some experience with it.