Tran Cuong
Tran Cuong

Reputation: 1

Use Object Destructuring without declaration an put it into a function

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

Answers (2)

Xceno
Xceno

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

Umair Farooq
Umair Farooq

Reputation: 1823

you can use console.log('visible', this.state.visible);. To use the destructing, you have to use two lines

Upvotes: 2

Related Questions