Author avatar

Manujith Pallewatte

Deeply Nested Objects and Redux

Manujith Pallewatte

  • Aug 30, 2019
  • 13 Min read
  • Aug 30, 2019
  • 13 Min read
Web Development


Managing the application state is one of the most discussed and debated topics in React. First, came the component state and soon it was not enough to keep up with the increasing complexities of SPA (Single Page Applications). Then, libraries such as React, Mobx, and Unstated emerged to simplify the process. While these libraries essentially ease the process of keeping the application state, they still required the developer to make sure that the changes to the state are done in a manner that respects the rule of immutable state.

In very simple terms, the React rendering process is sensitive only to the immutable updates of the state. This means that any change you make to object references is not propagated unless you explicitly create a new reference to the object, i.e. create a new object. With deeply nested objects, the workload (of both human and machine) significantly increases. This puts additional overhead for the developer to be cautious in the handling of state changes. But with a good initial state architecture, most of these problems can be avoided. We’ll explore most unavoidable circumstances and how to tackle them efficiently, in this guide.

Note: Although the guide topic is specific to the Redux state, this applies in general to React’s immutable state and most of the libraries helping to manage the application state.

Redux State Best Practices

Why not avoid a problem, if we can see it coming miles away? That's what the best practices are about. Redux documentation on Normalization State Shape gives a wide perspective on the matter. It suggests that we treat the application state as a relational database. Thus, we should apply the practices we apply in designing scalable database architecture. Let's look at the following scenario to get a better understanding of these best practices..

Scenario: We have to build the front-end for a blogging platform. It will have authors, who need to create posts. Each post will have comments. Each author will also have followers who are users of the platform. So, essentially, authors are users as well but with a flag to indicate that they are authors.

First, we'll try to store this data in the Unnormalized Form. One possible design would be as follows:

2    [
3        id: 1,
4        name: "author 01",
5        posts: [
6            {
7                title: "Bad Redux State",
8                body: ".....",
10                    {
11                        userId: 10,
12                        comment: "...."
13                    }
14                ],
15                tags: ["react", "redux"]
16                createdDate: ...,
17            }
18        ],
21        ]
22        createdDate: ...
23    ],
24    [
25        id: 2,
26        name: "author 02",
27        posts: [
28            {
29                title: "Post",
30                body: ".....",
32                    ...
33                ],
34                tags: ["react", "redux"]
35                createdDate: ...,
36            }
37        ],
38        createdDate: ...
39    ],

While this might seem like a good architecture when seen in the object form, let's convert it to a table and see if it still makes sense.

2| id | name | createdDate | post_01_title | post_01_body | post_01_tags | post_01_comment_01_body | post_01_comment_01_user | .... |
4| | | | | | | | | |

Now it looks absurd! It will look like this unless we normalize the table and apply the same to the application state. If you are familiar with the Database Normalization rules (1NF, 2NF, etc.) you can apply that knowledge. If not, we can use some basic rules to get the job done. Below is the set of guidelines from Redux's tutorial on this.

  1. Each type of data gets its own "table" in the state.

The first thing you need to do is to create a separate table for each entity type in the scenario. This means that we store users, posts, and comments in separate tables. In Redux, this would mean using separate reducers (or different sections on the same reducer). Note that authors and users are in fact in the same table. Also, tags are not separated, which is a design decision I chose. But it could be different according to the needs of the application.

1users : [...]
2comments: [...] 
3posts: [...]
  1. Each "data table" should store the individual items in an object, with the IDs of the items as keys and the items themselves as the values.
1users: [
2    {
3        "user01": {
4            id: "user01",
5            name: "John Smith",
6            ...
7        },
8        ...
9    }
12posts: [
13    {
14        "post01": {
15            id: "post01",
16            author: "user01",
17            title: "Good Redux State",
18            body: "...."
19        },
20        ...
21    }
  1. Any references to individual items should be done by storing the item's ID.

  2. Arrays of IDs should be used to indicate ordering.

The last guideline applies when you need a specific ordering to the set of objects. For example, comments should appear in an order that makes sense in the thread.

1comments: {
2    byIds: {
3        {
4            "comment01": {
5                id: "comment01",
6                comment: "...
7            },
8            "comment03": { ... },
9            "comment02": { ... },            
10        }
11    },
12    allIds: ["comment01", "comment02", "comment03", ...]

In the above example, allIds essentially gives us the list of keys in order so that we know in which order they should be displayed. Note that we don't (and can't in objects) need to maintain order in the actual objects.

By following the above guidelines, you essentially get a normalized application state which will:

  1. Simplify and flatten the state (less chance of nested state).
  2. Ease the access to different object properties.
  3. Increase UI performance because individual object properties can be updated without having to update intervening objects.

But in a real-world scenario, there will still be occurrences where the state contains nested objects and normalizing further doesn't make sense. In the next few sections, we'll explore how these instances can be tackled.

Using Deep Copy

For this guide, let's use the following hypothetical data structure that is supposed to be stored in the application state.

1const initState = {
2    firstLevel: {
3        secondLevel: {
4            thirdLevel: {
5                property1: ...,
6                property2: [...]
7            },
8            property3: ...
9        },
10        property4: ...
11    }

Also assume that this is already in the most normalized form. Now let's say we now need to update the value of property1 through an API call. Naturally, we would be inclined to directly update the states, as follows:

1// reducer.js
3const initState = {
4    ...
7export function rootLevelReducer(state, action){
9    const nestedState = state.firstLevel.secondLevel.thirdLevel;
10    nestedState.property1 =;
12    // this is similar to
13    // state.firstLevel.secondLevel.thirdLevel.property1 =
15    return state;

As I have commented, this change is not directly reflected in the state. Thus, Redux does not consider a rerender for the change. Note that an update to property4 will be rendered since it directly makes changes to the state object. For a property update of a deeply nested object to be rendered, the high-level reference needs to be changed. That is, a deep copy of the state object needs to be created with the desired changes to the nested properties done.

Although Object.assign() has been used for the purpose, it made the code largely unreadable. Thus came the Spread operator.

Note: It is important to know the benefits and pitfalls of using the spread operator. I suggest having a better understanding of the operator before overloading your application with it.

With the spread operator, we can simply use the following notation to correctly run the property update.

1const initState = {
2    ...
5export function rootLevelReducer(state, action){
6    return {
7        ...state,
8        firstLevel: {
9            ...state.firstLevel,
10            secondLevel: {
11                ...state.firstLevel.secondLevel,
12                thirdLevel: {
13                    ...state.firstLevel.secondLevel.thirdLevel,
14                    property1:
15                }
16            }
17        }
18    }

Although this solves our problem of updating deeply nested objects, it's apparent that the readability of the code falls significantly. So, we’ll try to improve readability by using nested reducers.

Using Nested Reducers

As the topic itself suggests, a nesting reducer calls can help with simplifying the above process. In the following example, we have created another reducer at the secondLevel to delegate the state changes of secondLevel and thirdLevel. And it decreases the overall complexity of using multiple levels of spread operators and increases readability.

1const initState = {
2    ...
5export secondLevelReducer(state, action){
6    return {
7        ...state,
8        thirdLevel: {
9            ...state.thirdLevel,
10            property1:
11        }
12    }
15export function rootLevelReducer(state, action){
16    return {
17        ...state,
18        firstLevel: {
19            ...state.firstLevel,
20            secondLevel: secondLevelReducer(state.firstLevel.secondLevel, action)
21        }
22    }

We can, of course, split this into three different reducer functions. It is a design decision that should be taken according to the situation. In this scenario, it felt like an overhead to create three separate reducer functions.

While these seem to make our lives easier, keeping track of the state structure becomes challenging in a sufficiently large codebase. So often we use the power of third-party libraries to further simplify these processes.

Using Immer

Immer.js is a "helper" library that simply creates a new state object by mutating the current "immutable state". Essentially, this abstracts the above deep copy operations (possibly with better optimizations) and lets us mutate the state without hassle. The theory behind the inner working of immer is beyond the scope of this guide. So, we will focus on its usage. First, we add immer to our project with:

1$ npm install --save immer

We simply use the produce method which provides us with a concept called a 'draft state'. In comparison to the redux state, the draft state is mutable. Once the mutations are done, immer compares the draft state to the original state and makes the changes accordingly. Let's fix immer into our deeply nested state.

1import produce from 'immer';
3const initState = {
4    ...
7export function rootLevelReducer(state, action){
8    return produce(state, draft => {
9        draft.firstLevel.secondLevel.thirdLevel.property1 =;
11        // bonus, you can do array updated as well!
12        // draft.firstLevel.secondLevel.thirdLevel.property2[index] = someData;
13    });

Success! The state updates are now as easy as a simple assignment. Using immer, you can simply assign the new value by directly accessing the required nested property. Moreover, you can change the value of individual array elements without having to copy all other array elements. I strongly suggest using immer (or any other library that provides similar functionality) at the very beginning of a project, if it has the potential to grow into a large codebase. The Redux Starter Kit published by the Redux team itself utilizes immer to provide easier inbuilt state mutation.


In today's guide, we explored the idea of structuring the application state of a React SPA. First, we briefly looked over the best practices in designing the state. While Redux suggests flattening the state as much as possible, we observed that there are unavoidable circumstances. For tackling these instances, this guide presented three different approaches.