talira

Work with immutable state by writing ordinary mutating code

open sourceMITfree

Immer is a small JavaScript library, created by Michel Weststrate, for working with immutable state. Instead of copying every level of an object by hand with spreads, you change a draft with ordinary JavaScript and its produce function returns a new state, sharing the parts that didn't change with the old one. It throws on accidental mutation and works with plain objects, arrays, Maps and Sets, so it fits React state, Redux reducers and configuration alike.

Problems it solves

  • update deeply nested state in React without writing spread operators at every level
  • stop accidental mutations of my Redux state
  • write immutable updates in JavaScript with ordinary assignment syntax

Features

  • produce: change a draft with normal mutating code and get a new immutable state
  • Structural sharing: unchanged parts of the tree are reused, not copied
  • Throws on accidental mutations of the frozen result
  • Works with plain objects, arrays, Maps and Sets
  • Curried producers for React setState and reducers

Alternatives

The same job

These do what Immer does.

  1. Immutable.js Immutable persistent data collections for Javascript which increase efficiency and simplicity
  2. MobX Simple, scalable state management
  3. Redux A JS library for predictable global state management
  4. Valtio Proxy-based state for React and vanilla JavaScript

Compare Immer with the top 3