Support functional updates for derived atoms
See original GitHub issueCurrently, setters of derived atoms do not support functional updates.
Proposed API:
const listAtom = atom([]);
const sortedListAtom = atom((get) => get(listAtom), (_get, set, update) => set(listAtom, update));
const App = () => {
const [list, setList] = useAtom(sortedListAtom);
return <button onClick={() => setList((prev) => [...prev, "oi!"])}>add</button>
};
In other words, is there a reason why the setter of derived atom isn’t a SetStateAction
same as for primitive atoms?
Issue Analytics
- State:
- Created 3 years ago
- Comments:8 (8 by maintainers)
Top Results From Across the Web
Unexpected store.sub behavior for derived atoms. #1014
I tried to map out the steps through the code below, with each indent somewhat representing a new call into a function. The...
Read more >Atom creators - Jotai
atomWithRefresh creates a derived atom that can be force-refreshed, by using the update function. This is helpful when you need to refresh asynchronous...
Read more >Core Concepts | Recoil
Atoms are units of state. They're updatable and subscribable: when an atom is updated, each subscribed component is re-rendered with the new value....
Read more >Jotai: The Ultimate React State Management - 100ms
Jotai, the atom-based state management for React. ... Changing the atoms it is derived from will automatically update this value.
Read more >Jotai: Atom-based state management for React
A big atom with small derived atoms ... This method is not the most atomic way of keeping the data but, This makes...
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
Ok, I understand why — the setter of the derived atom is basically a reducer, and as such you don’t want to prescribe what the
update
value is (same asuseReducer
). So this is a pickle.Yeah, for non-TS usage, we’d need to make the atom name descriptive, for example. Closing this as resolved.