Immutability Helpers
Redux is a tiny pattern that represents states as immutable objects. Redux was originally designed for React. Most Redux concepts, such as pure functions, are centered around the React ecosystem. Nowadays Redux is not directly related to React.
The cornerstone of Redux is immutability. Immutability is an amazing pattern to minimise unpredictable behaviour in our code. We're not going to cover functional programming in this article. However we're going to look at very useful packages that are called "immutability helpers".
The Problem
Most developers have to deal with, so called, "deep objects" and most important follow the immutability concept, when it comes to changing the value of some deeply nested property. Given the following code:
Let's imagine that we're faced with the task of changing the dueDate
property:
Let's see how we would implement the updateDueDate
action handler:
This code will work but unfortunately it is complicated to maintain and understand. It's not self-descriptive and will be daunting for new developers.
Solutions
There are different ways to improve this code. Let us look at a few different packages that can help in this regard.
State Operators
State operators are first-class immutability helpers that NGXS provides out of the box. The patch
operator will become your best friend in case of choosing state operators as your immutability helpers. Let's see how we could re-write the above code with the help of the patch
state operator:
immer
immer
is a very popular library that allows you to make changes to immutable objects as if they were mutable. The below code shows how to write the same code with the help of Immer:
Immer's produce
function can be also used as a state operator:
You may notice how much less code this is and how much better it looks. From the immer
repository:
Using Immer is like having a personal assistant; he takes a letter (the current state) and gives you a copy (draft) to jot changes onto. Once you are done, the assistant will take your draft and produce the real immutable, final letter for you (the next state).
immutability-helper
immutability-helper
is a small package that lets you mutate a copy of data without changing the original source:
immutability-helper repository
object-path-immutable
object-path-immutable
is a small library that allows you to modify deep object properties without modifying the original object. Let's look at how we could write the same code using this library:
object-path-immutable repository
immutable-assign
immutable-assign
is a lightweight library that pursues the same goal. Its syntax is similar to immer
's:
Ramda
Ramda is a great library for functional programming and it is used in a large number of projects. This example might be useful for people who use both Ramda and NGXS in their projects:
icepick
icepick
is a zero-dependency library for working with immutable collections. Given the following re-written code:
Summary
We have looked at several different libraries that might be helpful in accompanying the concept of immutability. Choose the right one for your needs.
Last updated