简体   繁体   中英

"Must use destructuring state assignment": How to destructure from object and place on property inside object literal

In a React project, I'm wanting to quickly troubleshoot things by logging specific parts of state at certain times.

console.error('this.state.thing', this.state.thing);

Doing this, My ESLint config gives me the error "Must use destructuring state assignment". So, I would have to either turn this ESLint rule off, or I would have to do this:

const { thing } = this.state;
console.error('this.state.thing', thing);

This is fine, but it made me wonder if I can destructure a property in the same way inside of an object literal in one go:

const objectLiteral = {
  thing: this.state.thing, // how to destructure thing out of state?
  stuff1,
  stuff2: otherData,
};

const somethingLikeThis = {
  thing: ({ thing } = this.state),
}

Just curious if there is a way to do this.

不在文字内部,但您可以将值分解为对象属性:

({thing: objectLiteral.thing} = this.state);

是的,你可以通过箭头函数来完成

console.error('this.state.thing', (obj => obj.thing)(this.state))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM