【发布时间】:2020-01-18 17:11:43
【问题描述】:
这是我编写的一些可以正常工作的示例代码:
useEffect(() => {
if (!rolesIsLoading && rolesStatus === 200) {
customer.roles = rolesData.roles;
}
}, [rolesIsLoading, rolesStatus]);
我在控制台中收到此警告:
React Hook useEffect 缺少依赖项:“customer.roles”和“rolesData.roles”。要么包含它们,要么删除依赖数组 react-hooks/exhaustive-deps
问题是,代码现在可以正常工作,并且在某些情况下,当我按照指示添加此类依赖项时,最终会出现无限循环或其他问题。
如果您遇到类似情况,请就如何解决此问题提出任何建议。
【问题讨论】:
-
customer和rolesData是什么?状态变量?还有什么?请包含更多代码。
标签: reactjs react-hooks use-effect