我收到以下错误
TypeError:无法读取未定义的属性“ setState”
closeAlert
似乎是绑定问题或道具问题? 所以我该怎么做?
主要组成部分:-
class App extends React.Component {
constructor(props) {
super(props);
this.state = {
alertOpen : false
};
this.closeAlert.bind(this);
}
closeAlert(event) {
this.setState({
closeAlert : true
});
}
render() {
return (
<div>
<AlertWindow
closeAlert={this.closeAlert}
/>
</div>
);
}
}
子组件:-
const AlertWindow = ({ closeAlert }) => {
return (
<Alert type="primary">
<Button
color="secondary"
RootComponent="button"
onClick={(event) => {
closeAlert(event)
}}
>
No, thanks
</Button>
</Button.List>
</Alert>
);
};
您的bind语句不正确,请将其更改为以下内容:
您需要将绑定函数分配给class方法。没有分配绑定就不够了。
希望有帮助!