immer

Create the next immutable state by mutating a temporary draft of the current one.

immer

General JS

Create the next immutable state by mutating a temporary draft of the current one.

Fit

Bundle (gzip)

6.5 kB

18.1 kB raw

Install size includes transitive dependencies. Bundle size is gzipped browser payload.

Freshness

The Problem

Updating nested immutable state with spread operators ({ ...state, a: { ...state.a, b: ... } }) is verbose, easy to get wrong, and obscures intent — yet mutating state directly breaks change detection in React and Redux.

What It Does

immer lets you write straightforward "mutating" code against a draft and produces a correctly structured-shared immutable result. It powers Redux Toolkit's reducers and integrates cleanly with React state.

Installation

bash
npm install immer

Usage Example

js
import { produce } from "immer";

const state = { user: { name: "Ada", tags: ["admin"] } };

const next = produce(state, (draft) => {
  draft.user.tags.push("editor");
  draft.user.name = "Ada Lovelace";
});

console.log(state.user.tags);  // ["admin"] (unchanged)
console.log(next.user.tags);   // ["admin", "editor"]

Related packages