[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:
- Created 3 years ago
- Reactions:5
- Comments:26 (22 by maintainers)
Top 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 >
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
@Aslemammad Maybe because the typing is tricky. Also, please note I didn’t test it at all, and count on you.
Notice #95 changed the src folder structure. Now, it’d be like: