question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

get loadable atom initial data without fetch

See original GitHub issue
const 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:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:9 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
drarmstrcommented, Oct 29, 2020

no! i want no call async function! just get value if selector has. but noWait call async function.

For that, you could set the default value of listAtom to null, then explicitly make your async call when you actually want to and then set the value of listAtom to the results when available.

1reaction
BenjaBobscommented, Oct 28, 2020

You can use noWait to get a loadable instead of waiting for a value: https://recoiljs.org/docs/api-reference/utils/noWait

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found