<分区>
如果我有一个设置状态并将其传递给子级的父级,我如何才能从子级更改父级的状态?
// parent
this.state = {
status: 'doing...'
}
// child render
<div>{this.props.state.status}</div>
// child function triggered by an onClick
update = () => this.setState(status: 'finished');
这样的东西是行不通的
// parent
this.state = {
status: this.props.state.status
}
// or child
this.props.setState(status: 'finished');
// or child
getDerivedStateFromProps(nextProps) {
console.log('I'm never executed)
this.setState({ status: nextProps.state.status});
}