[英]React Hook useEffect has a missing dependency: 'dispatch'. Either include it or remove the dependency array react-hooks/exhaustive-deps
can anyone help me to get rid of this warning谁能帮我摆脱这个警告
React Hook useEffect has a missing dependency: 'dispatch'. React Hook useEffect 缺少依赖项:'dispatch'。 Either include it or remove the dependency array react-hooks/exhaustive-deps
包括它或删除依赖数组 react-hooks/exhaustive-deps
here is my code这是我的代码
function Register(props) { const inusers=useSelector( (state) => {return state}); const history=useHistory() const dispatch=useDispatch(); // const formik = useFormikContext(); useEffect( () => { dispatch(fetchUser()) },[])
Just add dispatch among the dependencies:只需在依赖项之间添加调度:
useEffect(() => {
dispatch(fetchUser())
},[dispatch])
Dispatch is a safe dependency to add and it won't cause infinite loops. Dispatch 是一个安全的依赖项,它不会导致无限循环。 For more insights on why dispatch can change (and when it can change) check:
有关为什么调度可以更改(以及何时可以更改)的更多见解,请检查:
What are the cases where Redux dispatch could change? Redux 调度在哪些情况下会发生变化?
UPDATE: from react-redux documentation更新:来自 react-redux文档
You can write comment above useEffect dependency eslint-disable-next-line react-hooks/exhaustive-deps
and it will stop complaining.你可以在 useEffect 依赖
eslint-disable-next-line react-hooks/exhaustive-deps
上面写评论,它会停止抱怨。
React guarantees that dispatch function identity is stable and won't change on re-renders. React 保证调度函数标识是稳定的,不会在重新渲染时发生变化。 This is why it's safe to omit from the useEffect or useCallback dependency list.
这就是为什么从 useEffect 或 useCallback 依赖项列表中省略是安全的。
So its safe to ignore this warning.所以忽略这个警告是安全的。
function Register(props) {
const inusers=useSelector( (state) => {return state});
const history=useHistory()
const dispatch=useDispatch();
// const formik = useFormikContext();
useEffect( () => {
dispatch(fetchUser())
// eslint-disable-next-line react-hooks/exhaustive-deps
},[])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.