3

I've a question regarding the useState hook since the docs say that it completely replaces the old state with the new one you provided.

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

Following that can I just destruct whatever property inside an object state and mutate it directly instead of shallow/deep copying it? I mean it doesn't make any sense to shallow/deep copy to mutate it when the old state will be discarded and replaced with a brand new one.

Correct me if I'm wrong because this part confuses me. Also, I made a little sandbox to demonstrate my point.

Mohamed Wagih
  • 1,246
  • 6
  • 17
  • 1
    No, we do not need to deep clone as we did in the olde days with class-based components. See [this question](https://stackoverflow.com/questions/56296041/react-updating-state-array-of-objects-do-i-need-to-deep-copy-the-array) and the accepted answer. – Martin Dec 07 '21 at 08:28

0 Answers0