Replies: 1 comment 1 reply
-
You can update the state based on the existing state in a selector in a couple ways. You could use the However, it's a good practice to have symmetry for the getter and setters of a selector. For example, here's a selector family which gets and sets the value of a counter:
But note that the above will set the new value with a new counter value where you originally wanted the setter to increment the value. That's not really setting a new value and is more like an action. For that you don't really need a selector abstraction at all and can just use an updater when setting the atom:
Note that this uses the updater form of the selector to ensure it is incrementing based on the current value and not a potentially stale value as of the rendering. Or, you can potentially simplify things more and use simpler values in the atoms by using an atom family for the pressed counter:
And you can update in your component like the following:
Or create an general incrementor callback:
Note that the question doesn't actually seem to reference JSON. But, it would also be possible to parse and stringify JSON encodings with a selector abstraction. |
Beta Was this translation helpful? Give feedback.
-
Hi All!
Is there a way to set or get a selector/selectorFamily for an atom to get or set a part of value when its a json ?
This is a detailed question on stackoverflow
https://stackoverflow.com/questions/73366715/react-js-recoil-set-get-on-select-selectfamily-for-specific-attribute
If the answer is no (please explain the reason) what is the best practice ?
Thanks in Advance
The actual question from StackOverflow is:
I use Recoil state management in ReactJS to preserve a keyboard letters data, for example
In functional component i use
each time the a keyboard letter pressed the pressedCounter I want to increased for corresponded letter like that
How to achieve that ? Does recoil have a way to get/set a specific part/sub of json attribute ? (without make another atom? - I want to keep "Single source of truth") Or do you have a suggetion better best practice to do that ?
Beta Was this translation helpful? Give feedback.
All reactions