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

Ani*_*nde 12 reactjs react-hooks

谁能帮我摆脱这个警告

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

这是我的代码

function Register(props) {
  const inusers=useSelector( (state) => {return state});
  const history=useHistory()
  const dispatch=useDispatch();
  // const formik = useFormikContext();


  useEffect( () => {
    dispatch(fetchUser())
  },[])
Run Code Online (Sandbox Code Playgroud)

And*_*zo1 23

只需在依赖项中添加调度:

  useEffect(() => {
       dispatch(fetchUser())
  },[dispatch])
Run Code Online (Sandbox Code Playgroud)

Dispatch 是一个安全的添加依赖项,它不会导致无限循环。有关调度为何可以更改(以及何时可以更改)的更多见解,请检查:

Redux 调度在什么情况下可能会发生变化?

更新:来自react-redux文档

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