react父组件通过props更新了子组件的state,同时触发子组件的action只触发一次

我的代码是这么写的,我想要在我的父级组件通过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>
        );
    }
}
阅读 3.7k
2 个回答

为什么action会从props中传进来,然后你需要贴出父控件如何传递props的代码

其实我是想知道你的action具体是操作了什么,因为的你循环很显然是

父 render-> action传递给子 -> 子willreceive中又操作了action,action又影响的父重新render,然后就有了循环。

楼主这个问题怎么解决的?

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题