简体   繁体   English

如何修复 React Redux 和 React Hook useEffect 缺少依赖项:'dispatch'

[英]How to fix React Redux and React Hook useEffect has a missing dependency: 'dispatch'

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

I use useDispatch() Hook from React Redux on a functional component like this:我在这样的功能组件上使用来自 React Redux 的useDispatch() Hook:

const Component = () => {
  const dispatch = useDispatch();
  const userName = useSelect(state => state.user.name);

  useEffect(() => {
    dispatch(getUserInformation());
  }, [userId]);

  return (
    <div>Hello {userName}</div>
  );
};

export default Component;

How to remove this warning without removing the dependency array react-hooks/exhaustive-deps which can be useful to avoid other errors.如何在不删除依赖数组 react-hooks/exhaustive-deps 的情况下删除此警告,这对于避免其他错误很有用。

To avoid that warning simply add dispatch to the dependency array.为了避免该警告,只需将dispatch添加到依赖项数组即可。 That will not invoke re-renders because dispatch value will not change.这不会调用重新渲染,因为调度值不会改变。

const Component = () => {
  const dispatch = useDispatch();
  const userName = useSelect(state => state.user.name);

  useEffect(() => {
    dispatch(getUserInformation());
  }, [userId, dispatch]);

  return (
    <div>Hello {userName}</div>
  );
};

export default Component;

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

I use useDispatch() Hook from React Redux on a functional component like this:我在类似这样的功能组件上使用React Redux的useDispatch() Hook:

const Component = () => {
  const dispatch = useDispatch();
  const userName = useSelect(state => state.user.name);

  useEffect(() => {
    dispatch(getUserInformation());
  }, [userId]);

  return (
    <div>Hello {userName}</div>
  );
};

export default Component;

How to remove this warning without removing the dependency array react-hooks/exhaustive-deps which can be useful to avoid other errors.如何在不删除依赖关系数组react-hooks / exhaustive-deps的情况下删除此警告,这对于避免其他错误很有用。

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

I use useDispatch() Hook from React Redux on a functional component like this:我在类似这样的功能组件上使用React Redux的useDispatch() Hook:

const Component = () => {
  const dispatch = useDispatch();
  const userName = useSelect(state => state.user.name);

  useEffect(() => {
    dispatch(getUserInformation());
  }, [userId]);

  return (
    <div>Hello {userName}</div>
  );
};

export default Component;

How to remove this warning without removing the dependency array react-hooks/exhaustive-deps which can be useful to avoid other errors.如何在不删除依赖关系数组react-hooks / exhaustive-deps的情况下删除此警告,这对于避免其他错误很有用。

Simply add dispatch to your dependency array or make the dependency array empty.只需将dispatch添加到您的依赖数组或使依赖数组为空。

First Case:第一种情况:

const Component = () => {
  const dispatch = useDispatch();
  const userName = useSelect(state => state.user.name);

  useEffect(() => {
    dispatch(getUserInformation());
  }, [userId, dispatch]);

  return (
    <div>Hello {userName}</div>
  );
};

export default Component;

Second Case:第二种情况:

const Component = () => {
  const dispatch = useDispatch();
  const userName = useSelect(state => state.user.name);

  useEffect(() => {
    dispatch(getUserInformation());
  }, []);

  return (
    <div>Hello {userName}</div>
  );
};

export default Component;

By adding these dependencies, your useEffect may cause re-rendering or not re-render at all.通过添加这些依赖项,您的 useEffect 可能会导致重新渲染或根本不重新渲染。 It all depends on your data and the context in which you use it.这完全取决于您的数据和使用它的环境。

Anyways, if you do not wish to follow both the above methods then //ts-ignore can work but I will not recommend this as this may create bugs in the long run.无论如何,如果您不希望同时遵循上述两种方法,那么 //ts-ignore 可以工作,但我不建议这样做,因为从长远来看这可能会产生错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM