Support reducers for primitive atoms
See original GitHub issueAllow primitive atoms to define reducers (setters), which will allow to transform the update. I don’t think you should need to create a pair of a primitive and a derived atoms (where you don’t export the primitive atom at all) just to accomplish this.
It seems it’s somewhat supported even now, but it’s undocumented and not typed correctly:
type List = { id: string; value: number }[];
const sortedListAtom = atom<List, List>([], (get, set, update) => {
return set(
sortedListAtom,
update.sort((a, b) => a.value - b.value)
);
});
Use case: transform updates so that callers don’t have to do it themselves in each call site. In the above example, we want to update the list and ensure it’s sorted. The updater may be much more complex and you don’t want to force (and ensure) consumers do that themselves.
Issue Analytics
- State:
- Created 3 years ago
- Comments:6 (6 by maintainers)
Top Results From Across the Web
Support reducers for primitive atoms #38 - pmndrs/jotai
Allow primitive atoms to define reducers (setters), which will allow to transform the update. I don't think you should need to create a...
Read more >atomWithReducer - Jotai
Large objects · Atom creators · Custom useAtom hooks. Support Repository Package Community Updates ... Primitive and flexible state management for React ...
Read more >Jotai vs. Recoil: What are the differences? - LogRocket Blog
There are some features that are Jotai-specific, like reducer atoms ( atomWithReducer and useReducerAtom from jotai/utils ), immer integration ( ...
Read more >Clojure Concurrency Tutorial for Beginners with Code Examples
In this tutorial, you'll learn 16 ways for doing concurrency in Clojure including how to start threads and how to communicate between them....
Read more >How to type Redux actions and Redux reducers in TypeScript?
With Typescript 2's Tagged Union Types you can do the following interface ActionA { type: 'a'; a: string } interface ActionB { type:...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I meant just adding the type (if we manage to fix it — I haven’t looked at it yet). If not, it can indeed live in utils as a helper method.
I might change my mind in the future, if there were a better way.