【发布时间】:2018-06-15 18:01:30
【问题描述】:
我有一个容器,我需要更改显示表单或显示成功页面的 UI 表单。
容器有一个 state.showSuccess,我需要 MyFormModule 才能调用容器来改变状态。
以下代码有效,但我收到以下警告:
JSX 属性不应使用
.bind()
如何在不使用 .bind() 的情况下使其工作?
...
const myPage = class extends React.Component {
state = { showSuccess: false };
showSuccess() {
this.setState({
showSuccess: true,
});
}
render() {
const { showSuccess } = this.state;
if (showSuccess) {...}
....
<MyFormModule showSuccess={this.showSuccess.bind(this)} />
【问题讨论】:
-
也许使用箭头函数?:
showSuccess={() => this.showSuccess()} -
@CRice 做到了,谢谢!请发布答案,以便我为您投票。
-
如果你的 JSX 中不需要箭头函数,你也可以在构造函数中绑定它
标签: javascript reactjs components