Skip to content

Good practice to separate mutations from components #1955

Answered by dai-shi
KrysKruk asked this question in Q&A
Discussion options

You must be logged in to vote

Store API is exposed for certain use cases, but I wouldn't recommend it in this case, because everything is within the React world.

If I understand your problem correctly, the "Jotai" way is using write-only atoms. (It's just one practices and not all Jotai users are required to follow.)

const countAtom = atom(0);

const incrementCountAtom = atom(null, async (get, set, arg) => {
  await wait(500);
  set(countAtom, p => p + 1);
});

function App() {
  const count = useAtomValue(countAtom);
  const incrementCount = useSetAtom(incrementCountAtom);

  return <button onClick={incrementCount}>{count}</button>
}

AFAIR, Recoil doesn't allow this pattern.

Replies: 3 comments 9 replies

Comment options

You must be logged in to vote
2 replies
@KrysKruk
Comment options

@dai-shi
Comment options

Answer selected by KrysKruk
Comment options

You must be logged in to vote
5 replies
@dai-shi
Comment options

@alirezamirian
Comment options

@dai-shi
Comment options

@alirezamirian
Comment options

@dai-shi
Comment options

Comment options

You must be logged in to vote
2 replies
@alirezamirian
Comment options

@dmaskasky
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
4 participants