Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why immutable state with redux

Tags:

redux

I'm learning redux and am struggling to understand why state has to be immutable. Could you provide me with an example, in code preferably, where breaking the immutable contract results in an not so obvious side effect.

like image 960
Baz Avatar asked Sep 06 '16 08:09

Baz


People also ask

Why is immutable data important when working with React Redux?

It means that once created data cannot be changed. It makes maintaining immutable data structures easier and more efficient. The tool supports data structure like: List, Map, Set and also structures that are not implemented in . js by default but can be very useful: OrderedMap, OrderedSet and Record.

Why should state be immutable?

Using immutable states allows us to write code that can quickly tell if the state has changed, without needing to do a recursive comparison on the data, which is usually much, much faster.

Why is immutable state important React?

Besides reduced memory usage, immutability allows you to optimize your application by making use of reference- and value equality. This makes it really easy to see if anything has changed. For example a state change in a react component.

What is the benefit of immutable?

Improved safety - Once you've verified the state of an immutable object, you can be confident that it will stay safe and valid (no background process will be able to change it without you knowing) Better caching - You can cache references to immutable objects since they won't change.


2 Answers

Redux was originally invented to demonstrate the idea of "time-travel debugging" - being able to step back and forth through the history of dispatched actions, and see what the UI looks like at each step. Another aspect is being able to live-edit the code, reload it, and see what the output looks like given the new reducer logic.

In order to be able to properly step back and forth between states, we need to make sure that reducer functions have no side effects. That means data updates need to be applied immutably. If a reducer function actually directly modifies its data, then stepping back and forth between states will cause the application to behave in an unexpected fashion, and the debugging effort will be wasted.

Also, the React-Redux library relies on shallow equality checks to see if the incoming data for a component has changed. If the data references are the same, then the wrapper components generated by connect assume that the data has not changed, and that the component does not need to re-render. Immutable data updates means that new object references are created, and thus connect will see that the data has changed and the UI needs to update.

like image 57
markerikson Avatar answered Dec 10 '22 19:12

markerikson


Two Ideas

There are two ideas about immutability that you need to understand:

  • Mutate the state only in the reducers
  • Using an immutable data structure

Mutate the state only in the reducers

Redux tries to ensure that you only mutate the state in the Reducers. This is important because it makes easier to think about your application data flow.

Let's say that a value is not displayed in the UI as you expected or that a value that should have changed still showing its original value.

In that case, you could just think about which reducer is causing the mutation and see what went wrong.

This makes thinking about Redux issues very simple and makes developers highly productive.

Sometimes you can mutate the state in a view or in an action by mistake. If you think about the life-cycle:

Action -> Reducer -> Views-> Action 

If the state changes in a view and then an action is triggered the state change could be override. This would make very hard to find out what is going on for developers. We solve this by mutating state only in the reducers.

Note: another nice thing is that reducers are pure functions and all the async stuff takes places in the actions/middleware. So all side effects take place in the actions/middleware layer. Actions are our connection with the external world (HTTP, Local Storage, etc.).

Using an immutable data structure

As I have already mentioned, sometimes you can mutate the state in a view or in an action by mistake. To reduce chances of this happening we can make state mutations explicit by using and immutable data structure.

Using immutable data structures also has performance benefits because we don't need to perform deep equality checks to check for mutations.

The most commonly used provider of immutable data structures is immutable.js.

like image 30
Remo H. Jansen Avatar answered Dec 10 '22 20:12

Remo H. Jansen