An alternative API to selectAtom
for impure atoms, allowing to keep the old value of a derived atom under certain conditions.
#2158
Unanswered
iwoplaza
asked this question in
Show and tell
Replies: 1 comment 6 replies
-
It doesn't sound impure. Why won't you have the valid value and wip value in an object with an atom, or with two atoms? |
Beta Was this translation helpful? Give feedback.
6 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
It might sometimes be helpful to create a derived atom which holds the latest value that met a certain condition. For example we want an atom that derives the latest -valid- value of an input, while still letting the user change the underlying data according to their will.
A usage example in a Markdown Editor:
In my implementation, the atom returns
atomWithHistory.Empty
if the atom getter returnsKeepOld
when no 'old' value exists.Mitigating micro-suspensions
This pattern also helps with mitigating Suspensions due to microtasks, for example:
Implementation
Beta Was this translation helpful? Give feedback.
All reactions