简体   繁体   English

如何处理关于 Reat Hooks 的 useEffect 的 eslint 缺少依赖项警告

[英]How to handle eslint missing dependencies warning on useEffect of Reat Hooks

I am using useEffect for initial data我将 useEffect 用于初始数据

export const ChannelConfig = (id) => {
    const history = useHistory();
    const dataUrl = "/channel/"+id;

    useEffect(() => {
        fetch(dataUrl + "/configure")
          .then((resp) => {
              if (resp.ok) {
                  return resp.json();
              } else {
                  handleError(resp, "Server");
              }
          })
          .then((data) => {
              setSrcValue(data);
              setEditValue(data);
          })
    }, []);

    ... ...

    function handleError(resp, entity) {
        resp.json().then((err) => {
            customToast.error(resp.status, err, entity);
            if (resp.status === 404) {
                history.push("/pages/error-404")
            }
        });
    }

And I got this warning我收到了这个警告

 React Hook useEffect has missing dependencies: 'dataUrl' and 'handleError'. Either include them or remove the dependency array  react-hooks/exhaustive-deps

Was I wrong about using useEffect?使用useEffect我错了吗?

And, additionally when I convert "function handleError" to "useCallback", I got missing dependencies warning message about "history" from eslint.而且,另外,当我将“function handleError”转换为“useCallback”时,我收到了来自 eslint 的关于“history”的缺少依赖项警告消息。

I use "useRef" react hooks, now the missing dependencies warning is gone.我使用“useRef”反应钩子,现在缺少依赖项警告消失了。 Is that proper using??这样使用合适吗??

export const ChannelConfig = (id) => {
    **const history = useRef(useHistory());**
    const dataUrl = "/channel/"+id;

    useEffect(() => {
        fetch(dataUrl + "/configure")
          .then((resp) => {
              if (resp.ok) {
                  return resp.json();
              } else {
                  handleError(resp, "Server");
              }
          })
          .then((data) => {
              setSrcValue(data);
              setEditValue(data);
          })
    }, [dataUrl]);

    ... ...

    const handleError = useCallback((resp, entity) => {
        resp.json().then((err) => {
            customToast.error(resp.status, err, entity);
            if (resp.status === 404) {
                **history.current.push("/pages/error-404")**
            }
        }, []);
    }

Missing dependency warning is to notify the user to avoid unintentional closure issues.缺少依赖警告是为了通知用户避免意外关闭问题。

You can disable the warning if you are absolutely sure what you are writing is correct and intentional如果您绝对确定您所写的内容是正确且有意的,则可以禁用该警告

or或者

You can choose to get around the warning by converting the function to use useCallback and then adding it as a dependency.您可以选择通过将 function 转换为使用 useCallback 然后将其添加为依赖项来绕过警告。 Note that the function also uses history which is provided to it from closure and hence the useCallback will also warn you to use it.请注意,function 还使用从关闭时提供给它的历史记录,因此 useCallback 也会警告您使用它。

You can add history to useCallback as dependency as it will not change您可以将历史记录添加到 useCallback 作为依赖项,因为它不会改变

export const ChannelConfig = (id) => {
    const history = useHistory();
    const dataUrl = "/channel/"+id;

    ...
    const handleError = useCallback(function(resp, entity) {
        resp.json().then((err) => {
            customToast.error(resp.status, err, entity);
            if (resp.status === 404) {
                history.push("/pages/error-404")
            }
        });
    }, [history]);

    useEffect(() => {
        fetch(dataUrl + "/configure")
          .then((resp) => {
              if (resp.ok) {
                  return resp.json();
              } else {
                  handleError(resp, "Server");
              }
          })
          .then((data) => {
              setSrcValue(data);
              setEditValue(data);
          })
    }, [handleError]);

    ... ...

Please check this post for more details on this problem: How to fix missing dependency warning when using useEffect React Hook?请查看这篇文章以获取有关此问题的更多详细信息: How to fix missing dependency warning when using useEffect React Hook?

暂无
暂无

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

相关问题 在 useEffect 依赖项中缺少 ref 时没有 eslint 警告 - No eslint warning when ref is missing in useEffect dependency 如何删除“React Hook useEffect has missing dependencies”警告 - How to remove the “React Hook useEffect has missing dependencies” warning Eslint React Hooks错误:eslint-plugin-react-hooks用尽详尽的警告警告useEffect中的功能依赖项 - Eslint React Hooks Error: eslint-plugin-react-hooks exhaustive deps warning for a function dependency in useEffect 我应该如何修复 eslint 警告(React Hook useEffect 缺少依赖项)和由警告引起的循环? - How should I fix eslint warning (React Hook useEffect has a missing dependency) and a loop caused by the warning? React hook - useEffect 缺少依赖项警告 - React hook - useEffect missing dependencies warning 如何解决 useEffect eslint 依赖警告? - how to resolve useEffect eslint dependency warning? ESLint:收到错误“React Hook useEffect has missing dependencies” - ESLint: Getting the error “React Hook useEffect has missing dependencies” 如何摆脱警告“React Hook useEffect 缺少依赖项......”同时保留(有意)一个空数组作为依赖项? - How to get rid of warning "React Hook useEffect has missing dependencies ..." while keeping (intentionally) an empty array as dependencies? React 钩子中缺少依赖项的 eslint 警告总是正确的吗? - Are eslint warnings for missing dependencies in React hooks always correct? React Hooks JS Lint 警告 useEffect 缺少依赖项 - React Hooks JS Lint warning useEffect has a missing dependency
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM