【发布时间】:2021-09-10 14:22:39
【问题描述】:
以下代码运行良好。每次props.myObj["test"]?.prop1?.data 更改时都会触发useEffect 的功能。
但是,我收到了警告React Hook useEffect has a missing dependency: 'props.myObj'. Either include it or remove the dependency array
我不能将它包含在依赖项数组中,因为它会导致无限循环(因为props.myObj 具有不断变化的嵌套对象)。
当然,我不能删除当前的依赖项。那么知道如何解决这个问题吗?
useEffect(() => {
const data = props.myObj["test"]?.prop1?.data || {};
if (data["first"]) {
setTotalData(data["first"].data.length);
}
}, [props.myObj["test"]?.prop1?.data]);
【问题讨论】:
标签: reactjs react-hooks