question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Support functional updates for derived atoms

See original GitHub issue

Currently, 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:closed
  • Created 3 years ago
  • Comments:8 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
dwellecommented, Sep 13, 2020

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 as useReducer). So this is a pickle.

0reactions
dai-shicommented, Sep 13, 2020

Yeah, for non-TS usage, we’d need to make the atom name descriptive, for example. Closing this as resolved.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found