Declaratively resetting atom on another atom's change using keyedAtom
#2576
Unanswered
probeiuscorp
asked this question in
Show and tell
Replies: 1 comment
-
You can use atomEffect for this. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
I've often wanted to reset an atom when another atom changes, for example resetting the page number when the query changes. I believe this is the only pattern I've seen do this declaratively. This is sort of a continuation of #2136 and #2239 and the abilities of
stable()
.The first ingredient is
stable()
, a convenience wrapper aroundWeakMap
and sort of like a weakatomFamily
:For example, given
const getID = stable(() => nanoid())
,getID
will always return the same string for the same object without leaking memory.The second ingredient is
redirectedAtom
(looks a bit like a monad join!):redirectedAtom
behaves like the atom its pointing to, soThe final ingredient is
keyedAtom
(looks a bit like a monad bind!):Put all together, it can be used like:
(example in codesandbox)
Beta Was this translation helpful? Give feedback.
All reactions