Author avatar

Gaurav Singhal

Resolving React Error, "Tried to merge an object, instead got [object Object]"

Gaurav Singhal

  • Oct 8, 2020
  • 6 Min read
  • 25 Views
  • Oct 8, 2020
  • 6 Min read
  • 25 Views
Web Development
Front End Web Development
Client-side Frameworks
React

Introduction

While working with state variables in React, you might run into an error stating Tried to merge an object, instead got [object Object]. This usually happens when you are directly modifying your state and altering its original structure incorrectly. For instance, your state variable has initialized an empty object and you are trying to assign an array to it directly. This guide demonstrates how you can alter or update your state correctly without running into any errors.

Modifying State in React

In React, the state is conceived to be immutable. This means that if you have this state in your component

1
2
3
state={
	name: 'Margo'
}
javascript

you can't mutate the state directly as shown below.

1
2
3
this.state={
	name:'Fuzzy'
}
javascript

You have to call the setState() method available on your state to update the state asynchronously. Not only does this immutability follow a structured React pattern of updating the state and re-rendering the DOM, but it also prevents you from writing bad code that can break your app. For instance, if your state is an object containing an array, you will call setState() to update that array, keeping the original structure of the state intact.

Example in React

In an empty React project, create a simple class component with some predefined state.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import React, { Component } from 'react'

export default class App extends Component {
  state={
    todos:[
      {id:0,"status": 0, "title": "Wash Dishes"},
      {id: 1,"status": 1, "title": "Play Piano"}
     ]
  }
  render() {
    return (
      <div>
      </div>
    )
  }
}
javascript

The above class contains a simple state object with a property called todos. todos is an array of objects where each object represents information about a particular todo. Let's say you got a list of new todos back from the server and you want to append those todos to your state so you can output it on the DOM.

1
2
3
4
5
6
componentDidMount(){
    var newTodos = [
      {id:2,"status": 0, "tite": "Cook Dinner"},
      {id: 3,"status": 0, "title": "Make Bed"}
    ];
 }
javascript

newTodos is what you want to append to your state. Consider the following code that novice React developers write while merging a new object with the state.

1
2
3
4
5
6
7
8
9
10
11
componentDidMount(){
    var newTodos = [
      {id:2,"status": 0, "tite": "Cook Dinner"},
      {id: 3,"status": 0, "title": "Make Bed"}
    ];
    var newState = {};
  
    newState = newTodos;
    console.log(newState);
    this.setState(newState);
}
javascript

You first create a newState variable as an empty object and assign it to newTodos. So now newState is an array of objects with data from newTodos. Then you call the setState() method and pass in the new state inside it directly. Your original state was

1
2
3
4
todos:[
      {id:0,"status": 0, "title": "Wash Dishes"},
      {id: 1,"status": 1, "title": "Play Piano"}
]
javascript

After merging newTodos into your state, you should expect your state to become

1
2
3
4
5
6
todos:[      
    {id:0,"status": 0, "title": "Wash Dishes"},      
    {id: 1,"status": 1, "title": "Play Piano"},
    {id:2,"status": 0, "tite": "Cook Dinner"},
    {id: 3,"status": 0, "title": "Make Bed"}
]
javascript

See if the new state actually looks like the above by logging it to the console inside componetDidUpdate()

1
2
3
componentDidUpdate(){
    console.log(this.state)
}
javascript

If you inspect the console you will see that your state has deviated its original structure and it looks something like this

1
2
3
4
5
[
    {id: 2, status: 0, tite: "Cook Dinner"},
    {id: 3, status: 0, title: "Make Bed"},
    todos: (2) [{}, {}]
]
javascript

This isn't what you were expecting. Each element of the newly added todos is a direct child property of your state instead of the todos property. Not only does this badly structure your state, but it also leaves your code more prone to future bugs and errors. Additionally, it also becomes difficult to output all the todos on the DOM, as you have to loop through the properties separately depending on their data structure. You might also run into the error Tried to merge an object, instead got [object Object ] while updating the state. The correct way to do this is by maintaining the original structure of your state .

1
this.setState({...this.state,todos:[...this.state.todos,...newTodos]});
javascript

Using the spread operator, you first copy the original state to a new state and append the newly added todos to the todos property of the state. If you see the updated state logged on the console then your state looks exactly how it should, containing one single property called todos that contains all four todos as elements of an array. Now you can also easily output all todos by simply iterating over the todos property of the state and outputting the property you wish to render on the DOM .

1
2
3
4
5
6
7
8
9
10
render() {
    return (
      <div>
       {
         this.state.todos.map(todo=><p key={todo.id}>{todo.title}</p>)
       }
      </div>
    )
  }
}
jsx

Have a look at the entire code below.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import React, { Component } from 'react'

export default class App extends Component {
  state={
    todos:[
      {id:0,"status": 0, "title": "Wash Dishes"},
      {id: 1,"status": 1, "title": "Play Piano"}
     ]
  }

  componentDidMount(){
    var newTodos = [
      {id:2,"status": 0, "tite": "Cook Dinner"},
      {id: 3,"status": 0, "title": "Make Bed"}
    ]; 
  
     this.setState({...this.state,todos:[...this.state.todos,...newTodos]});
  }
  componentDidUpdate(){
    console.log(this.state)
  }
  render() {
    return (
      <div>
       {
         this.state.todos.map(todo=><p key={todo.id}>{todo.title}</p>)
       }
      </div>
    )
  }
}
jsx

Conclusion

Modifying or updating the state of your React components should be done in the right manner to avoid potential bugs in your code. Using JavaScript's spread operator, the state can be modified in a cleaner and shorter syntax. A lot of good practices can be adopted by simply learning newer and more modern features of JavaScript and using them in your code. They help you write clean, short, readable, and maintainable code and mitigate your chances of running into errors.

0