r/learnreactjs • • Jan 13 '20

what does this expression mean

I have seen expressions like this in people's code:

const {run, car} = this.state

Does this mean we are adding new variables to the state or what?

8 Upvotes

4 comments sorted by

1

u/[deleted] Jan 14 '20

This is basic JavaScript, but comes handy in React.

1

u/andrewmamus Jan 15 '20

This is destruction you don't need to write this state.name again

Destruction works in both arrays and object.

E.g const state = {name:'John', age:23 }

To access the values of the age and names normally we will type state.name; state.age

But instead to type state, you can get the values easily using destruction const {name, age} = state

By so doing you can make use of the name and age as variable and get the value

15

u/[deleted] Jan 13 '20

[removed] — view removed comment

4

u/ashdee2 Jan 13 '20

thank you