[英]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.