简体   繁体   English

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

I am getting this warning and I was wondering what steps I need to take to remove the warning from my console.我收到此警告,我想知道需要采取哪些步骤才能从控制台中删除警告。 The issue being, I am using the fetchProfile() function elsewhere so I can't move it within the useEffect.问题是,我在别处使用了 fetchProfile() 函数,所以我无法在 useEffect 中移动它。 any suggestions?有什么建议? My code is as follows:我的代码如下:

  const fetchProfile = async () => {
    const token = await localStorage.FBIdToken
    await axios
      .get(`/user`, {
        headers: {
          Authorization: `${token}`
        }
      })
      .then(res => {
        setUser(res.data)
        setFormData({
          ...formData,
          github: res.data.user.github ? res.data.user.github : '',
          website: res.data.user.website ? res.data.user.website : '',
          linkedIn: res.data.user.linkedIn ? res.data.user.linkedIn : '',
          cohort: res.data.user.cohort,
          program: res.data.user.program
        })
      })
      .catch(err => console.log('error'))
  }

  useEffect(() => {
    fetchProfile()
  }, [])

Wrapping the function in useCallback and adding it as a dependency should work, like so:将函数包装在useCallback并将其添加为依赖项应该可以工作,如下所示:

const fetchProfile = useCallback(async () => {
  // Do request as per normal
}, [])

useEffect(fetchProfile, [fetchProfile])

Another option is to move the fetchProfile function outside the component and pass in the setters to the call:另一种选择是将fetchProfile函数移到组件,并将 setter 传递给调用:

// This is defined outside your component
const fetchProfile = async ({ setUser, setFormData }) => {
  // Do request as per normal
}

useEffect(() => {
  fetchProfile({ setUser, setFormData })
}, [])

You're getting the warning because you're using fetchProfile inside your hook, but not including it in the dependencies.您收到警告是因为您在钩子中使用了fetchProfile ,但未将其包含在依赖项中。 If fetchProfile changes, your effect won't run.如果fetchProfile更改,您的效果将不会运行。

To get rid of the warning, add fetchProfile to the dependencies (the second argument to useEffect() ):要消除警告,请将fetchProfile添加到依赖项( useEffect()的第二个参数):

useEffect(() => {
  fetchProfile()
}, [fetchProfile])

暂无
暂无

声明:本站的技术帖子网页,遵循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 缺少依赖项:“url”。 包括它或删除依赖数组 react-hooks/exhaustive-deps - React Hook useEffect has a missing dependency: 'url'. 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 缺少依赖项:“loadAllProducts”。 包括它或删除依赖数组 react-hooks/exhaustive-deps - React Hook useEffect has a missing dependency: 'loadAllProducts'. Either include it or remove the dependency array react-hooks/exhaustive-deps 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
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM