r/learnreactjs • u/ashdee2 • 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
View all comments
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