我正在使用 Reactjs 16.10.2 制作网站。但我不能设置状态。
首先,我将函数作为组件的props传递:
getStepFour = (data)=>{
this.setState({
tenCaThi:data.tenCaThi,
ngayThi:data.ngayThi,
gioBatDau:data.gioBatDau,
gioKetKetThuc:data.gioKetKetThuc,
phongThi:data.phongThi,
});
console.log('data receipt four: ', data);
console.log('state after set: ', this.state.tenCaThi);
}
<ChiaCaThi getStepFour={this.getStepFour}/>
然后我用它在 ChiaCathi 中获取数据,如下所示:
componentWillUnmount = ()=>{
const data = {
tenCaThi:this.state.tenCaThi,
ngayThi:this.state.ngayThi,
gioBatDau:this.state.gioBatDau,
gioKetKetThuc:this.state.gioKetKetThuc,
phongThi:this.state.phongThi,
}
console.log('data in chiacathi: ', data)
this.props.getStepFour(data);
}
对于某些 setState 不起作用。设置后的状态为空。