get loadable atom initial data without fetch
See original GitHub issueconst fetchList = (): Promise<typeof LIST> =>
new Promise((resolve) => {
setTimeout(() => resolve(LIST), 100);
});
const fetchItem = (id: string) =>
new Promise((resolve) => {
setTimeout(() => resolve(LIST.find((item) => item.id === id)), 100);
});
const listAtom = atom({
key: "listAtom",
default: selector({
key: 'listAtom/Default',
get: async () => {
console.log('executed!');
return await fetchList()
}
})
});
const itemAtom = atomFamily({
key: "itemAtom",
default: selectorFamily({
key: "listAtom/Default",
get: (id: string) => async ({ get }) => {
const list = get(listAtom) || [];
if ("find" in list) {
const item = (list as any[]).find((d) => d.id === id);
if (item) {
return item;
}
}
return await fetchItem(id);
}
})
});
const Test = () => {
const data = useRecoilValueLoadable(itemAtom("1"));
return <>{data.state}</>;
};
then, executed!
is logged
can i get listAtom
’s value or null without async call??
Issue Analytics
- State:
- Created 3 years ago
- Reactions:1
- Comments:9 (3 by maintainers)
Top Results From Across the Web
Asynchronous Data Queries - Recoil
Recoil allows you to seamlessly mix synchronous and asynchronous functions in your data-flow graph of selectors. Simply return a Promise to a value...
Read more >What's the appropriate way load async (over the network) into ...
My first thought is to create a useEffect hook. In it I'd fetch the data, then run a for-loop and call atomFamily to...
Read more >Exploring Asynchronous Requests in Recoil - AppSignal Blog
Recoil has a useRecoilValue hook that fires the initial request and throws an exception when the component is not wrapped around <Suspense />...
Read more >TanStack Query - Jotai
React Query is often described as the missing data-fetching library for React, ... The first one is called dataAtom and it's an atom...
Read more >React + Recoil - Set atom state after async HTTP GET or ...
HTTP requests to the API are sent with the fetch wrapper. A React hook function is required because Recoil hook functions (e.g. ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
For that, you could set the
default
value oflistAtom
tonull
, then explicitly make your async call when you actually want to and then set the value oflistAtom
to the results when available.You can use
noWait
to get a loadable instead of waiting for a value: https://recoiljs.org/docs/api-reference/utils/noWait