Skip to main content
InterviewHubPRO
Section Overview
Q357

Question 357 of 407

Part 9: React Hooks Deep Dive (262 - 316)

88%
React Core & Hooks (407 Qs)•Part 9: React Hooks Deep Dive (262 - 316)

What are the possible ways of updating objects in state?

junior LevelCode ExampleHooks
  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,
          },
        }));