Skip to content

Can an atom be initialised on the first render? #581

Closed Answered by aulneau
Nazzanuk asked this question in Q&A
Discussion options

You must be logged in to vote

You could do this:

const myCountAtom = atomFamily(initialValue =>  atom(initialValue));

const useAtomHook = ({ initialCount = 2 }) => {
  const [count, setCount] = useAtom(myCountAtom(initialCount))

  return { count }; // returns 2 on first render
};

however, the atom will need to be referenced by the original "initialValue", eg if you wanted to use it elsewhere, you'd have to pass 2 (in this example) to the atomFamily.

Replies: 1 comment 3 replies

Comment options

You must be logged in to vote
3 replies
@aulneau
Comment options

aulneau Jul 6, 2021
Collaborator

@Nazzanuk
Comment options

@dai-shi
Comment options

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