[英]Reactjs eslint rule must use destructuring state assignment
class MyDrawer extends Component {
const modalOpen = this.state;
render() {
return (
<div>
<Drawer
modal
open={this.state.modalOpen} // <-- this line show error [eslint] Must use destructuring state assignment (react/destructuring-assignment)
onClose={() => this.setState({ modalOpen: false })}
>
</Drawer>
</div>
);
}
}
export default MyDrawer;
I tried change the line to be const { modalOpen } = this.state;
我尝试将行更改为
const { modalOpen } = this.state;
but now Failed to compile. 但现在无法编译。
How can i follow the rule and edit my code to be Compiled successfully? 如何遵循规则并编辑我的代码才能成功编译?
const
is in wrong place. const
在错误的地方。 Only class members can be declared inside class MyDrawer extends Component {...}
, and const
is not allowed there. 只有类成员可以在
class MyDrawer extends Component {...}
,并且不允许使用const
。 Destructuring assignment should reside inside a function where a variable should be available: 解构赋值应该驻留在一个变量应该可用的函数中:
render() {
const { modalOpen } = this.state;
return (
<div>
<Drawer
modal
open={modalOpen}
onClose={() => this.setState({ modalOpen: false })}
>
</Drawer>
</div>
);
}
Try this: 试试这个:
class MyDrawer extends Component {
const { modalOpen } = this.state; //fixed
render() {
return (
<div>
<Drawer
modal
open={ modalOpen } // fixed
onClose={() => this.setState({ modalOpen: false })}
>
</Drawer>
</div>
);
}
}
export default MyDrawer;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.