获取不是一个函数(或),如果我绑定我得到绑定的未定义的错误,我应该如何绑定这样的函数传递一个值?我在这里错过什么..?需要帮助。不是一个函数和绑定的未定义的错误reactjs
子组件:
constructor(props) {
super(props);
this.handler = this.handler.bind(this);
this.state = {
isBoolFlag: true,
}
}
onClick(e){
this.setState({
isBoolFlag: !this.state.isBoolFlag,
});
this.props.handler(!this.state.isBoolFlag); //Error here..
}
render() {
return (
<div>
<a onClick={this.onClick.bind(this)}>
</div>
);
}
}
父组件:
constructor(props) {
super(props);
this.handler = this.handler.bind(this);
this.state = {
showModule: false,
};
}
<div>
{this.state.showModule ? <Child2 /> : <Child1 handler={this.handler} />}
</div>
感谢您的回复。我已经在Parent中定义了,但是我仍然得到了错误this.props。处理程序不是函数(...)。感谢您指出,我也刚刚意识到setState不会立即反映出来。 – monkeyjs
没问题:)你能否在你的问题中添加Parent的组件代码或者至少是函数的代码? –
我已经添加了家长的组件代码也问题,让我知道如果你不能看到它..只是再次更新问题http://stackoverflow.com/users/6499571/c%c3%a9sar-landesa – monkeyjs