Dependencies 避免ESLint';react hooks/deps';具有使用效果的警告&;使用状态

Dependencies 避免ESLint';react hooks/deps';具有使用效果的警告&;使用状态,dependencies,eslint,use-effect,use-state,Dependencies,Eslint,Use Effect,Use State,我正在使用useffect()hook通过“useState”调用更新某些状态: useffect( () => { const newProps={} departments.forEach(dept=>{ 常量{code}=dept const newCode=code.toLowerCase() newProps[newCode+'\u审阅者']='' }) setFormValues({…formValues,…newProps}) }, [部门] ) 毫不奇怪,ESLint给了我一个

我正在使用
useffect()
hook通过“useState”调用更新某些状态:

useffect(
() => {
const newProps={}
departments.forEach(dept=>{
常量{code}=dept
const newCode=code.toLowerCase()
newProps[newCode+'\u审阅者']=''
})
setFormValues({…formValues,…newProps})
},
[部门]
)
毫不奇怪,ESLint给了我一个关于formValues的警告:

React Hook useEffect has a missing dependency: 'formValues'. 
然而,它接着提到:

Either include it or remove the dependency array.
You can also do a functional update 'setFormValues(f => ...)'
if you only need 'formValues' in the 'setFormValues' call

react-hooks/exhaustive-deps
我对神秘的“setFormValues(f=>…)”方法很感兴趣。这到底意味着什么?我应该如何重新编写
setFormValues({…formValues,…newProps})
以避免出现ESLint警告,而不添加
formValues
作为依赖项或添加
//ESLint禁用下一行react hooks/deps


我试过查看repo,最接近的是,它似乎没有提到这个特殊的解决方法。

您可以将一个函数传递给set函数,该函数的第一个参数是该状态的当前值,并将该状态设置为您在该函数中返回的值

因此,您可以改为:

setFormValues(fValues => ({...fValues, ...newProps}));
正如你所不知道的;不要在useEffect上的任何位置使用formValues状态,您可以避免将其添加到deps数组中