Skip to content

Write only atom that returns (prevState) => {} - setAction #2553

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

You must be logged in to vote

There are some ways depending on your requirement with the write-only atom, but this should help:

const userWriteAtom = atom(null, (get, set, arg) => {
  const prevUser = get(currentUserAtom);
  const nextUser = typeof arg === 'function' ? arg(prevUser) : arg;
  set(currentUserAtom, nextUser);
});

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@AlekseyP18
Comment options

Answer selected by AlekseyP18
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