Immer lets you edit draft copies of plain objects and arrays while producing new immutable snapshots. It fits React, Redux, and other state-heavy JavaScript code.
Overview
Immer is a small JavaScript package that makes working with immutable data structures less verbose. Instead of manually spreading and cloning nested objects at every level, you pass existing state and a recipe function that mutates a draft proxy; Immer records those changes and returns the next immutable state while sharing unchanged subtrees in memory.
The library uses standard objects, arrays, Sets, and Maps with familiar mutable APIs, then freezes results to catch accidental later changes. It detects illegal mutations outside produce and can throw, which helps guard reducers and component state updates.
Immer supports structural sharing, deep updates without hand-written copy logic, JSON patch generation, and a compact runtime footprint. Teams adopt it alongside React state, Redux reducers, and configuration objects where reference equality drives efficient rendering or change detection.
Internally, edits apply to a draft proxy of the current state until the recipe finishes, at which point Immer materializes the next tree. This model reduces boilerplate compared with manual immutable update patterns while keeping the immutability guarantees those patterns were meant to enforce.
Key features
- produce helper with draft-based recipes
- Automatic copies only where needed
- Works with plain JS collections
- Detects accidental mutations
- Structural sharing of unchanged data
- Freezes output objects
- JSON patch support
- Small gzipped bundle size
Use cases
- Simplify Redux reducer updates
- Update nested React component state
- Manage immutable configuration trees
- Enable cheap change detection by reference