react 父组件state改变后,通过props传到子组件时props没改变

 this.setState({
            Tochecked: [...packageIds],
            ToUnchecked: [],
            changeIds: [...ids],
            historyDiagnosis:[...historyDiagnosisChecked] ,
            bizCategoryDiagnosis:[...bizCategoryDiagnosisListChecked] ,
        }, async () => {
            await CustomConfirm({
                title: '您确定要订阅全部的行业包吗?',
                content: '',
                okText: '确认',
                cancelText: '取消',
                onOk() {
                    _this.onSubscribeSubmit()
                },
                onCancel() {
                    _this.setState({
                        Tochecked: [],
                        ToUnchecked: [],
                        changeIds: [],
                        historyDiagnosis: _this.state.historyDiagnosisSaved,
                        bizCategoryDiagnosis: _this.state.bizCategoryDiagnosisSaved,
                    })
                },
            })
        })
   historyDiagnosis.map((item: any, index: number) => {
                                    return <SubscribeCard key={index} parent={this} packData={item} descHidden={true} cardStyle={cardStyle} />
                                })

item已经改变了,但子组件通过compoentReceiverProps生命周期拿不到最新的值,需要用getDerivedStateFromProps来替换才可

    static getDerivedStateFromProps(nextProps:any, prevState:any) {
        const {packData} = nextProps;
        if (packData.checkded !== prevState.packDataChild.checked) {
            return {
                packDataChild:packData,
            };
        }
        // 否则,对于state不进行任何操作
        return null;
    }

版权声明:本文为weixin_44233592原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。