0

If I need to make a copy of state to mutate in a function, do the following function the same?

const { mana } = this.state;
const mana = { ...this.state.mana };
Taouen
  • 65
  • 7
  • Does this answer your question? [Destructuring state/props in React](https://stackoverflow.com/questions/52055987/destructuring-state-props-in-react) – Danial Aug 27 '21 at 14:00
  • No, I know that the destructuring statement works. What I want to know is if the spread syntax functions identically. – Taouen Aug 27 '21 at 14:03

1 Answers1

1

Those do not do the same thing. The first line doesn't do any copying, it's just a shorthand for const mana = this.state.mana. Your second line makes a shallow copy of this.state.mana, and then assigns the new object to mana.

Nicholas Tower
  • 72,740
  • 7
  • 86
  • 98