在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),
}
只是好奇是否有办法做到这一点。
答案 0 :(得分:1)
不在文字内,但是您可以将值分解为对象属性:
({thing: objectLiteral.thing} = this.state);
答案 1 :(得分:0)
是的,您可以通过箭头功能来实现
console.error('this.state.thing', (obj => obj.thing)(this.state))