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 reducers for primitive atoms

See original GitHub issue

Allow 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)
  );
});

Sandbox

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

github_iconTop GitHub Comments

1reaction
dwellecommented, Sep 13, 2020

Although I wouldn’t add this in core

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.

0reactions
dai-shicommented, Sep 13, 2020

Although I wouldn’t add this in core

I might change my mind in the future, if there were a better way.

Read more comments on GitHub >

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

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