【发布时间】:2018-10-19 12:05:25
【问题描述】:
如果我有一个设置状态并将其传递给子级的父级,我如何从子级更改父级的状态?
// 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});
}
【问题讨论】:
标签: javascript reactjs react-props