![](/img/trans.png)
[英]React - componentWillReceiveProps does not execute when component rendered in unit test
[英]componentWillReceiveProps on react does not pass the current property when ever it triggers
大家好,我是新来的人,我正在与React组件一起从父组件的状态传递属性,但我不确定为什么每次从父组件触发事件时都会收到未定义的属性错误
您可以在此处访问代码@ https://codepen.io/private_ryan/pen/RVBdpO?editors=0011#live-view显示控制台,然后单击“编辑”按钮
SampleTable组件
constructor(props, context) {
super(props);
this.state = { UPD:[] };
}
updateRow(x) {
var array = this.state.TRs;
var index = array.findIndex(e => e.id == x);
this.setState({
UPD: this.state.TRs[index]
});
}
render() {
return (<AddFormData onAdd={ this.onAddForm }
upd={ this.state.UPD }
updcan={ this.cancelUpd }
propUpd= { this.propcessUpd } />
<button onClick={ this.updateRow} value={ some.id } >click me</button>
);
}
AddFormData组件
constructor(props) {
super(props);
this.state = { textName: '', textArea: '' };
}
componentWillReceiveProps(){
console.log( this.props ) // undefined no props when first click
// set the state here
}
接收到新的道具作为该函数的参数:
componentWillReceiveProps(nextProps)
https://facebook.github.io/react/docs/react-component.html#componentwillreceiveprops
componentWillReceiveProps
每当你在父组件道具值进行任何更改将被调用,新的价值将获得通过的参数和该生命周期方法之后this.props
将得到更新,所以如果你console.log(this.props)
这里面它会记录前一个值而不是新值。
用这个:
componentWillReceiveProps(newProps){
console.log(this.props.upd.id, newProps)
}
检查工作示例 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.