Skip to main content

Command Palette

Search for a command to run...

Self-review: Managing state within a component - Advanced React

Updated
1 min readView as Markdown
Self-review: Managing state within a component - Advanced React
D

A passionate full-stack developer from @ePlus.DEV

  1. Is this a valid useState hook invocation and destructuring?

     const [car, setCar] = useState({ color: 'blue', mileage: 0})
    
    • Yes

    • No

    • It would be valid, if it was spread over multiple lines.

  2. True or False: You can clone a JS object using the . operator (the dot operator).

    • True

    • False

  3. Consider the following code:

     const [person, setPerson] = useState({ name: 'John', age: 21})
    

    Imagine you're using a setPerson() state-updating function to update the value of the state variable named person. You only want to update the value of age, from 21 to 22. Choose the correct code snippet to do that.

    • setPerson(prev => ({ ...prev, age: 22 }));

    • setPerson(() => ({ age: 22 }));

    • setPerson(person.age = 22);


More from this blog

E

ePlus.DEV - Exploring Technology with David Nguyen

1241 posts

A passionate full-stack developer from VIETNAM.