in React JS by
Q:

What are the possible ways of updating objects in state?

1 Answer

0 votes
by
  1. Calling setState() with an object to merge with state:

    • Using Object.assign() to create a copy of the object:

      const user = Object.assign({}, this.state.user, { age: 42 })
      this.setState({ user })
    • Using spread operator:

      const user = { ...this.state.user, age: 42 }
      this.setState({ user })
  2. Calling setState() with a function:

    this.setState(prevState => ({
      user: {
        ...prevState.user,
        age: 42
      }
    }))
Click here to read more about React JS
Click here to read more about Insurance

Related questions

0 votes
asked Dec 8, 2020 in React JS by SakshiSharma
0 votes
asked Nov 26, 2019 in React JS by AdilsonLima
0 votes
asked May 20, 2020 in React JS by SakshiSharma
0 votes
asked Jun 3, 2020 in MongoDB by Robindeniel
0 votes
0 votes
asked Nov 26, 2019 in React JS by AdilsonLima
0 votes
...