我的代码是这么写的,我想要在我的父级组件通过props更新了子组件的state后只执行一次子组件的action。请问 我这么写是正确的么?
这么写我的action 会一直循环执行
componentWillReceiveProps(nextProps) {
this.setState({
index: nextProps.selectType
}, () => {
// this.props.actions.submitAppealTypeFn(this.state);
});
}
父组件的代码
class FillName extends Component {
constructor(props) {
super(props);
this.state = {
selectType: ''
};
}
handleSelectType(type) {
this.setState({
selectType: type
});
}
render() {
const {fillAppealName, actions} = this.props;
console.log(this.state);
return (
<div className="component-fillaccount">
<Header/>
<StepFillName
{...this.state}
actions={actions}/>
<Footer/>
<AlertForm
handleSelect={this.handleSelectType.bind(this)}
stateObj={fillAppealName}
actions={actions}/>
</div>
);
}
}
为什么action会从props中传进来,然后你需要贴出父控件如何传递props的代码
其实我是想知道你的action具体是操作了什么,因为的你循环很显然是
父 render-> action传递给子 -> 子willreceive中又操作了action,action又影响的父重新render,然后就有了循环。