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.

is there a difference between atomFamily default and selectorFamily get?

See original GitHub issue

Was a bit confused by this two approaches.

const userInfoQuery = atomFamily({
    key: 'UserInfoQuery',
    default: async userID => {
        const response = await myDBQuery({userID});
        return response;
  });

and

const userInfoQuery = selectorFamily({
    key: 'UserInfoQuery',
    get: userID => async () => {
        const response = await myDBQuery({userID});
        return response;
    },
});

They both will be recomputed if we pass new userID. So if we don’t need get or set then we can use atomFamily?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:11 (1 by maintainers)

github_iconTop GitHub Comments

3reactions
drarmstrcommented, Jun 2, 2020

Both expose the same interface and both may be read/writeable (if the selector provides a set).

Atoms represent and maintain state while selectors represent functions. Use atoms when you need state and a selector when it is derived from other state or async requests.

In reality, atoms with asynchronous defaults are actually implemented by wrapping with a selector. It’s provided as a convenience. So, to your original question, the atomFamily would actually construct selectors to get the async data as well as atoms with state that then go unused if you never set the atom.

2reactions
alexandrzavaliicommented, Jun 2, 2020

@drarmstr hmm interesting. In the docs we have this selector

const userNameQuery = selectorFamily({
  key: 'UserName',
  get: (userID) => async ({get}) => {
    const response = await myDBQuery({userID});
    if (response.error) {
      throw response.error;
    }
    return response.name;
  },
});

It was missleading to see the get there even though it is not used. Would it be okay to remove it? Have this pr here: https://github.com/facebookexperimental/Recoil/pull/234

Read more comments on GitHub >

github_iconTop Results From Across the Web

atomFamily(options) - Recoil
default - The initial value of the atom. Like an atom, it may either be a value directly or a Promise , Loadable...
Read more >
What is the use case of atomFamily in recoil? - Stack Overflow
The Cell component uses useRecoilState and find its specific Atom by accessing the main dictionary using the key it got passed as a...
Read more >
atomFamily() - Recoil 中文文档
default : The initial value of the atom. It may either be a value directly, a RecoilValue or Promise that represents the default...
Read more >
atomFamily — Jotai, primitive and flexible state management ...
initializeAtom is function that can return any kind of atom ( atom() ... To reproduce the similar behavior to Recoil's atomFamily/selectorFamily, ...
Read more >
Use selectorFamily to take arguments in your Recoil selectors
[0:25] To start, we're going to store our sortValue in an atom. Again, we'll give it the key sortValue, and we will give...
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