|
|
|
|
|
by acemarke
2144 days ago
|
|
Thanks! Quick summary: Immer is an incredibly useful immutable update library, created by Michel Weststrate (author of MobX). It exports a single function `produce(originalState, updateCallback)`. The callback receives a `draftState` value that _looks_ like your original state, but has been wrapped in an ES6 Proxy. You can then "mutate" the draft all you want. Internally, Immer tracks all the mutations, and the final result is a safely immutably-updated value. It drastically simplifies immutable update logic - no more nested spread operators! See https://immerjs.github.io/immer/docs/introduction and https://redux.js.org/recipes/structuring-reducers/immutable-... . Redux Toolkit comes with Immer built into our `createReducer` and `createSlice` APIs automatically. |
|
But then a couple of weeks later I had to wrangle data in subsections of three “slices“ of state at the same time.
Immer lets you focus on just the state you want to change, and hides the problem of maintaining the state you don’t want to change. Brilliant. I’ll take the “it looks like mutation” hit for that.