0

What is the difference between these two? Are they the same? Is there any idea related to the pointer? Please help thanks! (I could not find any related topics, and this is not related to the destructuring assignment. Please don't close my post)

const items = this.state.items <br>
const {items} = this.state
Drew Reese
  • 165,259
  • 14
  • 153
  • 181
Yan Skywalker
  • 119
  • 1
  • 10
  • 3
    These are equivalent statements and have nothing to do with hooks - they are simply two examples of assignment in ES6. – Chris Heald Aug 11 '22 at 06:31
  • "this is not related to the destructuring assignment" — You're asking what the difference between code that uses a destructuring assignment and code which does exactly the same thing without using a destructuring assignment is!! – Quentin Aug 11 '22 at 07:23

1 Answers1

0

The first is regular variable assignment

const items = this.state.items;

The seconds is destructuring assignment

const { items } = this.state;

The destructuring assignment syntax is a JavaScript expression that makes it possible to unpack values from arrays, or properties from objects, into distinct variables.

IMO the only significant difference between the two is the amount of code you write. Destructuring assignment is handy when you need to unpack several object properties.

Compare

const propA = this.state.propA;
const propB = this.state.propB;
const propB = this.state.propC;

versus

const { propA, propB, propC } = this.state;
Drew Reese
  • 165,259
  • 14
  • 153
  • 181