【发布时间】:2018-08-16 22:10:43
【问题描述】:
我是 React 世界的新手。我在执行编辑操作时被生命周期方法卡住了。
即使改变了 props 也不会触发 componentWillReceiveProps 方法。
这是我的代码示例。
this.intialState = {
name:'',
code:'',
};
this.state = this.intialState;
componentWillReceiveProps(nextprops){
this.setState({
name:nextprops.KaraikarEdit.name,
code:nextprops.KaraikarEdit.code
})
}
render() {
return (
<div className="form-group">
<input type= "text" className="form-control" value={this.state.name} placeholder="Enter Name" name="name" onChange = {this.onhandleChange} />
</div>
<div className="form-group">
<input type= "text" className="form-control" value={this.state.code} placeholder="Enter Code" name="code" onChange = {this.onhandleChange} />
</div>
);
}
const mapStateToProps = (state) => {
return {
KaraikarEdit: state.KaraikarDetails.editdata
}
}
export default connect(mapStateToProps)(KaraikarMasterAdd);
我在 KaraikarEdit 中收到了新道具。但是 componentWillReceiveProps 生命周期方法没有被触发。
我知道 componentWillReceiveProps 在第一次渲染时没有被调用。所以我使用了 componentDidMount 方法,在这个方法中我调度了一个对该组件无用的操作。然后 componentWillReceiveProps 正在工作。
我应该使用 componentDidMount 还是 componentWillMount 方法来使用 componentReceiveProps 方法?如果是这样,在 componentDidMount 方法中包含什么?
【问题讨论】:
-
是的。我发送了一个操作并使用 editdata 更新了我的商店。只有从州我得到了道具。但是仍然没有触发 componentWillReceiveProps。
-
你如何确定你在 karaikarEdit 中收到了新道具? componentwillreceiveprops 是一个仅在更新场景中触发而不在初始渲染中触发的事件(如您所说)。因此,如果您的组件在初始渲染后发送了新的道具,它肯定可以工作
-
当然。我在 KaraikarEdit 中收到了新道具。
-
您如何确认您在 karaikarEdit 中收到了 newProps?
-
@arthy,你的应用是做什么的?
标签: reactjs react-redux