reactjs - 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();
}
}
解决方案
ComponentWillReceiveProps
不更新道具。当父级重新渲染或您连接到 redux 并且您的商店得到更新时,它实际上会接收更新的道具。无论如何使用 ComponentWillReceiveProps 是不安全的。现在我们在这里有一个替代品,getDerivedStateFromProps
.
推荐阅读
- python-3.x - 带有 .txt 文件的 CSV 阅读器
- javascript - 将函数名称移动到外部配置文件/模块时,函数未定义错误
- c++ - VSCode C/C++ Intellisense 是否完整的类成员?
- kotlin - 我将如何为模块提供动态令牌?
- apache-kafka - Spring kafka 中的事务
- javascript - 如何确定Javascript中项目网格中选择范围之间的重叠
- javascript - node.js 生成响应时出错。TypeError:response.json 不是函数
- php - PHP分页活动页码未突出显示
- javascript - 获取有问题的反应组件方法
- multithreading - 没有大量内存分配的 cython 的 prange 中的线程局部数组