Immer (German for: always) is a tiny package that allows you to work with immutable state in a more convenient way.
The Immer package exposes a produce function that does all the work. ... produce takes a base state, and a recipe that can be used to perform all the desired mutations on the draft that is passed in.
Redux + Immer is extensively covered in the documentation of Redux Toolkit. For Redux without Redux Toolkit, the same trick as applied to useReducer above can be applied: wrap the reducer function with produce, and you can safely mutate the draft!
API overview ... Importing immer In most cases, the only thing you need to import from Immer is produce: ... Note that in older versions, produce was also available as default export (e.g. import produce from "immer" was also valid, but that is no longer the case to improve eco system compatibility.
Provides installation instructions for Immer, a library for creating immutable states in JavaScript applications.
The Immer package ships with type definitions inside the package, which should be picked up by TypeScript and Flow out of the box and without further configuration. The TypeScript typings automatically remove readonly modifiers from your draft types and return a value that matches your original type. See this practical example:
Working with immutable data, before Immer, used to mean learning all the immutable update patterns.
Q: How does Immer work? Q: How does Immer work? Read the (second part of the) introduction blog. Q: Does Immer use structural sharing? So that my selectors can be memoized and such? A: Yes Q: Does Immer support deep updates? A: Yes Q: I can't rely on Proxies being present on my target environments. Can I use Immer? A: Yes - view details Q: Can I typecheck my data structures when using Immer? A ...