Reputation: 1
I'm coding a React App and just curious: is there a way to use object destructuring without a declaration (using let, const, var) and right away put it into a function?
I have tried this but failed.
console.log('visible', {visible}: this.state);
My state for example:
this.state = {
visible: true
}
From (2 lines)
let {visible} = this.state;
console.log('visible', visible);
To (1 line)
console.log('visible', {visible} = this.state);
Upvotes: 0
Views: 168
Reputation: 913
If you're writing your own function you can destructure the arguments directly like this (TypeScript):
const someFunc = ({visible}:SomeState) => {//etc.}
If you don't use TypeScript you don't need the :SomeState
Upvotes: 0
Reputation: 1823
you can use console.log('visible', this.state.visible);
. To use the destructing, you have to use two lines
Upvotes: 2