[英]"Must use destructuring state assignment": How to destructure from object and place on property inside object literal
在 React 項目中,我想通過在特定時間記錄狀態的特定部分來快速解決問題。
console.error('this.state.thing', this.state.thing);
這樣做,我的 ESLint 配置給了我錯誤“必須使用解構狀態分配”。 所以,我要么關閉這個 ESLint 規則,要么我必須這樣做:
const { thing } = this.state;
console.error('this.state.thing', thing);
這很好,但它讓我想知道我是否可以一次性在對象文字中以相同的方式解構一個屬性:
const objectLiteral = {
thing: this.state.thing, // how to destructure thing out of state?
stuff1,
stuff2: otherData,
};
const somethingLikeThis = {
thing: ({ thing } = this.state),
}
只是好奇是否有辦法做到這一點。
不在文字內部,但您可以將值分解為對象屬性:
({thing: objectLiteral.thing} = this.state);
是的,你可以通過箭頭函數來完成
console.error('this.state.thing', (obj => obj.thing)(this.state))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.