简体   繁体   English

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

I am trying to update my reducer every time a useSelector change its value, but I need to use the spread operator to keep the rest of the content of my reducer.每次 useSelector 更改其值时,我都试图更新我的减速器,但我需要使用扩展运算符来保留减速器的其余内容。 But my useEffect gives the following warning:但是我的 useEffect 给出了以下警告:

React Hook useEffect has a missing dependency: 'formValues'. React Hook useEffect 缺少依赖项:'formValues'。 Either include it or remove the dependency array react-hooks/exhaustive-deps.要么包含它,要么删除依赖数组 react-hooks/exhaustive-deps。

Is there a way to avoid this warning and keep the rest of state of my reducer?有没有办法避免这个警告并保持我的减速器的其余状态?

My code useEffect code:我的代码使用效果代码:

 const handleDispatch = useCallback( values => { dispatch(hrRequestHiringFormChangeValues(values)); }, [dispatch] ); useEffect(() => { if (cepAddressValues.error !== true) { handleDispatch({ ...formValues, state: cepAddressValues.state, city: cepAddressValues.city, neighborhood: cepAddressValues.neighborhood, address: cepAddressValues.street, }); } else { handleDispatch({ ...formValues, state: '', city: '', neighborhood: '', address: '', }); } }, [cepAddressValues, handleDispatch]);

in dependency array of hooks like useEffect, useCallback, useMemo you have to place all variables that are declared outside of this hook and you used it inside of hook function.在像 useEffect、useCallback、useMemo 这样的钩子依赖数组中,你必须放置在这个钩子之外声明的所有变量,并在钩子函数内使用它。 In this case you use formValues in handleDispatch inside useEffect but you don't place it in dependency array.在这种情况下,您在 useEffect 内的 handleDispatch 中使用 formValues 但您没有将其放置在依赖项数组中。

[cepAddressValues, handleDispatch, formValues]

Disable react-hooks/exhaustive-deps inline内联禁用react-hooks/exhaustive-deps

(make sure that dependency is not required)

  useEffect(() => {
    if (cepAddressValues.error !== true) {
      handleDispatch({
        ...formValues,
        state: cepAddressValues.state,
        city: cepAddressValues.city,
        neighborhood: cepAddressValues.neighborhood,
        address: cepAddressValues.street,
      });
    } else {
      handleDispatch({
        ...formValues,
        state: '',
        city: '',
        neighborhood: '',
        address: '',
      });
    }
  // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [cepAddressValues, handleDispatch]);

Check, is exist react-hooks plugin in eslint plugins Thanks检查,eslint 插件中是否存在react-hooks插件谢谢

暂无
暂无

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

相关问题 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 如何解决`react-hooks/exhaustive-deps`的`React Hook useEffect缺少依赖`? - How to solve `React Hook useEffect has a missing dependency` of `react-hooks/exhaustive-deps`? 反应 useEffect 带有警告的钩子 react-hooks/exhaustive-deps - React useEffect hook with warning react-hooks/exhaustive-deps ESLint 希望 setSate 作为 useEffect 的依赖,但这会导致无限循环(react-hooks/exhaustive-deps) - ESLint wants setSate as a dependency for useEffect but this causes an infinite loop (react-hooks/exhaustive-deps) useEffect 和 'react-hooks/exhaustive-deps' linting - useEffect and 'react-hooks/exhaustive-deps' linting 带有自定义 IntersectionObserver 钩子的 react-hooks/exhaustive-deps 警告 - react-hooks/exhaustive-deps warning with custom IntersectionObserver hook React Hook useEffect 缺少依赖项:'formData'。 包括它或删除依赖项数组。 什么是依赖就是使用 - React Hook useEffect has a missing dependency: 'formData'. Either include it or remove the dependency array. what is dependency is use React Hook useEffect 缺少一个依赖项:'handleLogout'。 要么包含它,要么移除依赖数组 react - React Hook useEffect has a missing dependency: 'handleLogout'. Either include it or remove the dependency array react react-hooks/exhaustive-deps 导致依赖警告,修复挂起代码 - react-hooks/exhaustive-deps causing dependency warning, fix hangs code React Hook useEffect 缺少依赖项:'fetchTracker'。 包括它或删除依赖项数组 - React Hook useEffect has a missing dependency: 'fetchTracker'. Either include it or remove the dependency array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM