#
Immutable Update PatternsThe articles listed in Prerequisite Concepts#Immutable Data Management give a number of good examples for how to perform basic update operations immutably, such as updating a field in an object or adding an item to the end of an array. However, reducers will often need to use those basic operations in combination to perform more complicated tasks. Here are some examples for some of the more common tasks you might have to implement.
#
Updating Nested ObjectsThe key to updating nested data is that every level of nesting must be copied and updated appropriately. This is often a difficult concept for those learning Redux, and there are some specific problems that frequently occur when trying to update nested objects. These lead to accidental direct mutation, and should be avoided.
#
Correct Approach: Copying All Levels of Nested DataUnfortunately, the process of correctly applying immutable updates to deeply nested state can easily become verbose and hard to read. Here's what an example of updating state.first.second[someId].fourth
might look like:
Obviously, each layer of nesting makes this harder to read, and gives more chances to make mistakes. This is one of several reasons why you are encouraged to keep your state flattened, and compose reducers as much as possible.
#
Common Mistake #1: New variables that point to the same objectsDefining a new variable does not create a new actual object - it only creates another reference to the same object. An example of this error would be:
This function does correctly return a shallow copy of the top-level state object, but because the nestedState
variable was still pointing at the existing object, the state was directly mutated.
#
Common Mistake #2: Only making a shallow copy of one levelAnother common version of this error looks like this:
Doing a shallow copy of the top level is not sufficient - the nestedState
object should be copied as well.
#
Inserting and Removing Items in ArraysNormally, a Javascript array's contents are modified using mutative functions like push
, unshift
, and splice
. Since we don't want to mutate state directly in reducers, those should normally be avoided. Because of that, you might see "insert" or "remove" behavior written like this:
However, remember that the key is that the original in-memory reference is not modified. As long as we make a copy first, we can safely mutate the copy. Note that this is true for both arrays and objects, but nested values still must be updated using the same rules.
This means that we could also write the insert and remove functions like this:
The remove function could also be implemented as:
#
Updating an Item in an ArrayUpdating one item in an array can be accomplished by using Array.map
, returning a new value for the item we want to update, and returning the existing values for all other items:
#
Immutable Update Utility LibrariesBecause writing immutable update code can become tedious, there are a number of utility libraries that try to abstract out the process. These libraries vary in APIs and usage, but all try to provide a shorter and more succinct way of writing these updates. For example, Immer makes immutable updates a simple function and plain JavaScript objects:
Some, like dot-prop-immutable, take string paths for commands:
Others, like immutability-helper (a fork of the now-deprecated React Immutability Helpers addon), use nested values and helper functions:
They can provide a useful alternative to writing manual immutable update logic.
A list of many immutable update utilities can be found in the Immutable Data#Immutable Update Utilities section of the Redux Addons Catalog.
#
Simplifying Immutable Updates with Redux ToolkitOur Redux Toolkit package includes a createReducer
utility that uses Immer internally.
Because of this, you can write reducers that appear to "mutate" state, but the updates are actually applied immutably.
This allows immutable update logic to be written in a much simpler way. Here's what the nested data example
might look like using createReducer
:
This is clearly much shorter and easier to read. However, this only works correctly if you are using the "magic"
createReducer
function from Redux Toolkit that wraps this reducer in Immer's produce
function.
If this reducer is used without Immer, it will actually mutate the state!. It's also not obvious just by
looking at the code that this function is actually safe and updates the state immutably. Please make sure you understand
the concepts of immutable updates fully. If you do use this, it may help to add some comments to your code that explain
your reducers are using Redux Toolkit and Immer.
In addition, Redux Toolkit's createSlice
utility will auto-generate action creators
and action types based on the reducer functions you provide, with the same Immer-powered update capabilities inside.