简体   繁体   English

React Hook useEffect 缺少依赖项:'dispatch'。 包括它或删除依赖数组 react-hooks/exhaustive-deps

[英]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文档

https://react-redux.js.org/api/hooks

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.

相关问题 React Hook useEffect 缺少依赖项要么包含它,要么删除依赖项数组 react-hooks/exhaustive-deps - React Hook useEffect has a missing dependency Either include it or remove the dependency array react-hooks/exhaustive-deps React Hook useEffect 缺少依赖项:'user.id'。 包括它或删除依赖数组 react-hooks/exhaustive-deps - React Hook useEffect has a missing dependency: 'user.id'. Either include it or remove the dependency array react-hooks/exhaustive-deps React Hook useEffect 缺少依赖项。 要么包含它,要么移除依赖数组 react-hooks/exhaustive-deps - React Hook useEffect has a missing dependency. Either include it or remove the dependency array react-hooks/exhaustive-deps React Hook useEffect 缺少依赖项:'fetchProfile'。 包括它或删除依赖数组 react-hooks/exhaustive-deps - React Hook useEffect has a missing dependency: 'fetchProfile'. Either include it or remove the dependency array react-hooks/exhaustive-deps React Hook useEffect 缺少依赖项:'formValues'。 包括它或删除依赖数组 react-hooks/exhaustive-deps - React Hook useEffect has a missing dependency: 'formValues'. Either include it or remove the dependency array react-hooks/exhaustive-deps React Hook useEffect 缺少依赖项:包含它或删除依赖项数组 react-hooks/exhaustive-deps - React Hook useEffect has a missing dependency: Either include it or remove the dependency array react-hooks/exhaustive-deps 第 93:6 行:React Hook useEffect 缺少依赖项:'estado'。 要么包含它,要么移除依赖数组 react-hooks/exhaustive-deps - Line 93:6: React Hook useEffect has a missing dependency: 'estado'. Either include it or remove the dependency array react-hooks/exhaustive-deps React Hook useEffect 缺少依赖项:'loading'。 包括它或删除依赖数组 react-hooks/exhaustive-deps - React Hook useEffect has a missing dependency: 'loading'. Either include it or remove the dependency array react-hooks/exhaustive-deps React Hook useEffect 缺少依赖项:xxx。 包括它们或删除依赖数组 react-hooks/exhaustive-deps - React Hook useEffect has missing dependencies: xxx. Either include them or remove the dependency array react-hooks/exhaustive-deps React Hook useEffect 缺少依赖项:“roomID”和“sotreId”。 要么包含它们,要么删除依赖数组 react-hooks/exhaustive-deps - React Hook useEffect has missing dependencies: 'roomID 'and 'sotreId'. Either include them or remove the dependency array react-hooks/exhaustive-deps
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM