首页 > 解决方案 > componentWillReceiveProps 如何在 react 中更新 props?

问题描述

下面是 componentWillReceiveProps 的一些代码片段。

这里 bulkUploadRptSuccess、bulkUploadRptError 是数组,bulkUploadRptException 是字符串。

因此,一旦状态更新两个数组和一个字符串,它就会打开弹出窗口。它按预期工作。

但是现在当点击任何东西进入应用程序时,每次都会打开弹出窗口。

如何比较 componentWillReceiveProps 中的条件数组检查。

如何在此函数内将两个数组值与相等或不进行比较谢谢,

    componentWillReceiveProps = (nextProps) => {

        let { OCFCheckConfig } = this.props;
        let { bulkUploadRptSuccess, bulkUploadRptError, bulkUploadRptException } = OCFCheckConfig;

        if (nextProps.OCFCheckConfig.bulkUploadRptSuccess.length > 0 || nextProps.OCFCheckConfig.bulkUploadRptError.length > 0) {

          this.addPopupOpen();
        }


       }

标签: reactjsreact-redux

解决方案


ComponentWillReceiveProps不更新道具。当父级重新渲染或您连接到 redux 并且您的商店得到更新时,它实际上会接收更新的道具。无论如何使用 ComponentWillReceiveProps 是不安全的。现在我们在这里有一个替代品,getDerivedStateFromProps.


推荐阅读