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.

[Tips] How to use with immer

See original GitHub issue

@gsimone suggested useImmerAtom:

const useImmerAtom = (anAtom) => {
  const [state, setState] = useAtom(anAtom)
  const setStateWithImmer = useCallback((fn) => {
    setState(produce((draft) => fn(draft)))
  }, [setState])
  return [state, setStateWithImmer]
}

Or, we could create a writable atom:

const countAtom = atom(0)

const countWithImmerAtom = atom(
  (get) => get(countAtom),
  (get, set, fn) => set(countAtom, produce((draft) => fn(draft)))
)

We might be able to create a helper function.

const warpWithImmer = (anAtom) => atom(
  (get) => get(anAtom),
  (get, set, fn) => set(anAtom, produce(get(anAtom), (draft) => fn(draft)))
)

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:5
  • Comments:26 (22 by maintainers)

github_iconTop GitHub Comments

2reactions
dai-shicommented, Oct 7, 2020

@Aslemammad Maybe because the typing is tricky. Also, please note I didn’t test it at all, and count on you.

1reaction
dai-shicommented, Oct 6, 2020

Notice #95 changed the src folder structure. Now, it’d be like:

  • ./src/immer.ts
  • ./src/immer/useImmerAtom.ts
Read more comments on GitHub >

github_iconTop Results From Across the Web

Better Reducers With Immer - Smashing Magazine
In this article, we're going to learn how to use Immer to write reducers. When working with React, we maintain a lot of...
Read more >
Using Immer for React State Management | CSS-Tricks
use -immer is a hook that allows you to manage state in your React application. Let's see this in action using a classic...
Read more >
Using Immer with React: a Simple Solutions for Immutable ...
Immer is a small library created to help developers with immutable state based on a copy-on-write mechanism, a technique used to implement a ......
Read more >
Immer performance - GitHub Pages
Here is a simple benchmark on the performance of Immer. This test takes 50,000 todo items and updates 5,000 of them. Freeze indicates...
Read more >
Introducing Immer: Immutability the easy way - Medium
Tip : if you don't like reading, you can also watch the egghead tutorial for immer ... Simplifying the reducer by using Immer....
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