Closed plumsirawit closed 4 years ago
If a selector is referencing previous executions, then that would actually represent additional state; selectors are really only functions of derived state, and not state themselves.. Select functions are "pure" or "idempotent" at a high-level concept. The output of the function should always be the same for a given set of input dependency values. We can't make guarantees about how many times it is executed or the order of execution. Using another atom to store "last valid" state or something would be a reasonable way to handle this. You can also use the set
of your selector wrapper to set both atoms with valid state changes.
Consider a hypothetical scenario when I have an atom
AState
(used to represent some text) and a selectorBState
which has a logic like this:BState
should return2 * AState
if and only ifAState
is a numberBState
should return the last thing it has ever returned beforeFor example, let
AState
has the default value of0
, now theBState
is0
. When I trigger set state of A to2
,BState
will be4
. But when I setAState
toHello, world!
,BState
should renders4
(as the last thing it returned before).As a direct attempt, I tried to use
get(BState)
directly inside the get function of the selector, but it turns out likeUncaught Error: Recoil selector has circular dependencies
.After a little thinking, I come up (but still not sure) with a solution of using two atoms, one for current value (let's call this
currentAState
) and the other one for last valid value (let's call thislatestValidAState
) and letBState
reference a derivation ofcurrentAState
if it is valid, orlatestValidAState
otherwise. But I think this solution does not utilize the benefits of recoil.So my questions are: