【发布时间】:2020-06-15 07:06:55
【问题描述】:
我需要一些帮助来了解 eslint react-hooks/exhaustive derps。
我只希望在listenToMe 发生更改时运行效果,但react-hooks/exhaustive derps 正在对我大喊要添加history。这会导致递归。在这条规则之前,useEffect 对我来说是 React 黄金。
ESLint:React Hook useEffect 缺少依赖项:'history'。要么包含它们,要么删除依赖数组。(react-hooks/exhaustive-deps)
谁能帮我理解:
- 为什么在 useEffect 中只监听您关心的更改是不好的做法?
- 什么是只监听状态变化的特定变化的“正确”方式?
useEffect(() => {
if (listenToMe) {
const search = new URLSearchParams(location.search);
search.delete('q')
history.replace({ ...location, search: search.toString() });
}
}
}, [listenToMe]);
我已经阅读了github并做出了反应,但我没有阅读任何点击的内容。
【问题讨论】:
-
React 的解释reactjs.org/docs/…
标签: reactjs react-hooks eslint