Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do I need to use the spread operator when using useState hook on object when updating?

I just started learning about hooks, and according to the official docs on Using Multiple State Variables, we find the following line:

However, unlike this.setState in a class, updating a state variable always replaces it instead of merging it.

So, if I understand correctly, this mean I don't need to use the spread operator for updating the state?

like image 276
Y_Moshe Avatar asked Feb 10 '20 19:02

Y_Moshe


People also ask

Does useState hook automatically merge update objects?

Unlike the setState method found in class components, useState does not automatically merge update objects. You can replicate this behavior by combining the function updater form with object spread syntax: setState(prevState => { // Object.

How do I update state with useState?

const [state, setState] = useState(initialState); To update the state, call the state updater function with the new state setState(newState) . Alternatively, if you need to update the state based on the previous state, supply a callback function setState(prevState => newState) .

How do you update an array of objects in useState?

myArray. push(1); However, with React, we need to use the method returned from useState to update the array. We simply, use the update method (In our example it's setMyArray() ) to update the state with a new array that's created by combining the old array with the new element using JavaScript' Spread operator.

Can you use useState with an object?

One of React's most commonly used Hooks is useState , which manages states in React projects as well as objects' states. With an object, however, we can't update it directly or the component won't rerender.

When should I use the spread operator to update state?

You never have to use the spread operator to update state. I'm guessing you're referring to when you need to clone objects or arrays and ensure you aren't mutating? You still don't want to mutate state. So if your state is an object, you'll want to create a new object and set with that. This may involve spreading the old state. For example:

How to use the usestate hook with objects?

We can use the useState hook with objects by passing in a callback to our state change functions that returns the copy of the existing state object and set the property to the value we want after that. ← How to Reduce the Number of Re-renders When Updating States? → How to Force a Component to Re-render with React Hooks?

Do I need to spread my state when using hooks?

That said, using hooks you often no longer need your state to be an object, and can instead use useState multiple times. If you're not using objects or arrays, then copying is not needed, so spreading is also not needed.

How to use the spread operator on an object?

Let’s take at an example of how to use the spread operator on an object, Here we are spreading the user1 object. All key-value pairs of the user1 object are copied into the clonedUser object. Let’s look on another example of merging two objects using the spread operator,


Video Answer


2 Answers

You still don't want to mutate state. So if your state is an object, you'll want to create a new object and set with that. This may involve spreading the old state. For example:

const [person, setPerson] = useState({ name: 'alice', age: 30 });

const onClick = () => {
  // Do this:
  setPerson(prevPerson => {
    return { 
      ...prevPerson, 
      age: prevPerson.age + 1 
    }
  })
  // Not this:
  //setPerson(prevPerson => {
  //  prevPerson.age++;
  //  return prevPerson;
  //});
}

That said, using hooks you often no longer need your state to be an object, and can instead use useState multiple times. If you're not using objects or arrays, then copying is not needed, so spreading is also not needed.

const [name, setName] = useState('alice');
const [age, setAge] = useState(30);

const onClick = () => {
  setAge(prevAge => prevAge + 1);
}
like image 124
Nicholas Tower Avatar answered Oct 19 '22 02:10

Nicholas Tower


What it means is that if you define a state variable like this:

const [myThings, changeMyThings] = useState({cats: 'yes', strings: 'yellow', pizza: true })

Then you do something like changeMyThings({ cats: 'no' }), the resulting state object will just be { cats: 'no' }. The new value is not merged into the old one, it is just replaced. If you want to maintain the whole state object, you would want to use the spread operator:

changeMyThings({ ...myThings, cats: 'no' })

This would give you your original state object and only update the one thing you changed.

like image 35
Seth Lutske Avatar answered Oct 19 '22 03:10

Seth Lutske