"react-hooks/exhaustive-deps": "warn" 对 eslint-plugin-react-hooks 太敏感了?
"react-hooks/exhaustive-deps": "warn" for eslint-plugin-react-hooks is too sensitive?
useEffect(() => {
const apiCall = async () => {
try {
let newSearchFormOptions = { ...searchFormOptions };
...
setSearchFormOptions(newSearchFormOptions);
} catch (e) {
setPageState({ error: e.error });
} finally {
setPageState({ isLoading: false });
}
};
apiCall();
}, []);
在我将eslint添加到项目之前,上面的代码没有eslint问题。在项目中添加eslint规则后,显示“React Hook useEffect has a missing dependency: 'searchFormOptions'”。然而,searchFormOptions 是状态,setSearchFormOptions 将更新它,这使得“apiCall”被一次又一次地调用。我知道我们可以使用 eslint 来禁用此警告,但我担心 eslint 规则是否过于敏感,而我计划在安装组件时仅触发一次“apiCall”。我们是否需要依赖 eslint 规则,或者我们需要自己仔细检查,然后使用 eslint 来禁用此警告,或者我们是否有任何其他解决方法?
提前致谢。
Quote 来自 React 团队成员:
You can put
// eslint-disable-next-line react-hooks/exhaustive-deps
before the violating line if there is a good reason. Usually disabling
it is a mistake and will significantly bite you later. (Most cases
where people think it's safe to disable are not safe and lead to stale
closures.)
有时当我知道我想要 运行 效果中的内容时 在挂载期间仅一次 ,我已将其禁用。
useEffect(() => {
const apiCall = async () => {
try {
let newSearchFormOptions = { ...searchFormOptions };
...
setSearchFormOptions(newSearchFormOptions);
} catch (e) {
setPageState({ error: e.error });
} finally {
setPageState({ isLoading: false });
}
};
apiCall();
}, []);
在我将eslint添加到项目之前,上面的代码没有eslint问题。在项目中添加eslint规则后,显示“React Hook useEffect has a missing dependency: 'searchFormOptions'”。然而,searchFormOptions 是状态,setSearchFormOptions 将更新它,这使得“apiCall”被一次又一次地调用。我知道我们可以使用 eslint 来禁用此警告,但我担心 eslint 规则是否过于敏感,而我计划在安装组件时仅触发一次“apiCall”。我们是否需要依赖 eslint 规则,或者我们需要自己仔细检查,然后使用 eslint 来禁用此警告,或者我们是否有任何其他解决方法?
提前致谢。
Quote 来自 React 团队成员:
You can put
// eslint-disable-next-line react-hooks/exhaustive-deps
before the violating line if there is a good reason. Usually disabling it is a mistake and will significantly bite you later. (Most cases where people think it's safe to disable are not safe and lead to stale closures.)
有时当我知道我想要 运行 效果中的内容时 在挂载期间仅一次 ,我已将其禁用。