Skip to content

Setter for async selector #1890

Answered by csantos42
sivasquared asked this question in Q&A
Jun 27, 2022 · 2 comments · 4 replies
Discussion options

You must be logged in to vote

Here's a few approaches I can think of, each with their own tradeoffs:

/**
 * Method 1: Atom with promise as default (will fetch immediately)
 */

const recoilAccountsState = atom({
  key: 'recoilAccounts',
  default: recoilAccountsApiCall(),
});

set(recoilAccountsState, (accounts) => [...accounts, newAccount]);

/**
 * Method 2: Atom with selector as default (will fetch lazily)
 */

 const recoilAccountsState2 = atom({
  key: 'recoilAccounts',
  default: selector({
    key: 'recoilAccountsSel',
    get: () => recoilAccountsApiCall(),
  }),
});

set(recoilAccountsState, (accounts) => [...accounts, newAccount]);

/**
 * Method 3: Bi-directional selector (will fetch lazily)
 */

const loca…

Replies: 2 comments 4 replies

Comment options

You must be logged in to vote
0 replies
Answer selected by sivasquared
Comment options

You must be logged in to vote
4 replies
@csantos42
Comment options

@sivasquared
Comment options

@csantos42
Comment options

@sivasquared
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants