InterviewHubPRO
Section Overview
Q394

Question 394 of 445

Part 11: Legacy Architecture, Migration Patterns & Historical Internals (354 - 445)

89%
React & Modern Frontend (445 Qs)•Part 11: Legacy Architecture, Migration Patterns & Historical Internals (354 - 445)

What are the possible ways of updating objects in state?

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

    css
    - Using `Object.assign()` to create a copy of the object:
    
javascript
          const user = Object.assign({}, this.state.user, { age: 42 });
          this.setState({ user });
markdown
    - Using _spread operator_:
javascript
          const user = { ...this.state.user, age: 42 };
          this.setState({ user });
javascript
2.  **Calling `setState()` with a function:**
javascript
        this.setState((prevState) => ({
          user: {
            ...prevState.user,
            age: 42,
          },
        }));