<分区>
基本上我想在子组件中调用父组件的函数。该函数将改变父组件的状态。
我在父组件中创建了一个处理程序并将其作为 prop 传递给子组件。 现在我想在子组件中调用它。
家长:
state = { formstep: '1'}
constructor(props) {
super(props)
this.handler = this.handler.bind(this)
}
handler(e) {
e.preventDefault()
this.setState({
formstep: '2'
})
}
render () {
return (
<Form1 handler = {this.handler} />
)
}
在 child 中,当我尝试调用处理函数时,它说
Cannot read property 'props' of null
child :
handleClick() {
//Saving Some data from form
//and calling parent function
this.props.handler;
}
render () {
return (
<button onClick={this.handleClick}>Continue</button>
)
}