admin管理员组文章数量:1131394
Given a simple hook function useFoo with a simple function foo.
When I use a function inside of useEffect, everything appears to function fine, but I get a waring from exhaustive-deps saying that function must be listed as a dependency.
When I add the function to the deps array it causes the effect to fire repeatedly or even continuously depending on the situation.
Why is foo not stable? Or how do I make the warning go away, and keep my code working properly?
const useFoo = () => {
const foo = () => {
return 'foo'
}
return {
foo
}
}
export default useFoo
Used in a control without dep
useEffect(() => {
console.log('i fire 2 times because of React.Strict')
}, [])
Used in a control with dep
useEffect(() => {
console.log('i fire many times')
}, [foo])
Given a simple hook function useFoo with a simple function foo.
When I use a function inside of useEffect, everything appears to function fine, but I get a waring from exhaustive-deps saying that function must be listed as a dependency.
When I add the function to the deps array it causes the effect to fire repeatedly or even continuously depending on the situation.
Why is foo not stable? Or how do I make the warning go away, and keep my code working properly?
const useFoo = () => {
const foo = () => {
return 'foo'
}
return {
foo
}
}
export default useFoo
Used in a control without dep
useEffect(() => {
console.log('i fire 2 times because of React.Strict')
}, [])
Used in a control with dep
useEffect(() => {
console.log('i fire many times')
}, [foo])
Share
Improve this question
edited 2 days ago
Jason Hernandez
asked Jan 8 at 16:57
Jason HernandezJason Hernandez
2,9711 gold badge19 silver badges31 bronze badges
2
|
2 Answers
Reset to default 1UseCallback needs to be added to the foo function to make it stable.
const useFoo = () => {
const foo = useCallback(() => {
return 'foo'
}, [])
return {
foo
}
}
const useFoo = () => {
const foo = () => { // <- this creates a new function every time the hook is called
return 'foo'
}
return { // <- this creates a new object every time the hook is called
foo
}
}
The function is completely stateless and independent of the enclosing hook. Consequently, it can be moved outside the hook so that only a single instance of it exists:
const foo = () => {
return 'foo'
}
const useFoo = () => {
return {
foo
}
}
Moving stateless and independent functions out of the hook is cleaner and cheaper than using yet another hook to cache the function instance (the function instances are still created when executing your hook, they are then simply discarded and ignored).
You can do the same with the object literal, which doesn't depend on anything from the hook:
const foo = () => {
return 'foo'
}
const fooObj = {
foo
}
const useFoo = () => {
return fooObj
}
But then the question becomes why do you have a hook in the first place if you don't use any hook functionality? Just call your foo
function directly.
本文标签:
版权声明:本文标题:reactjs - Why does useEffect fire multiple times after adding functions in warnings from the exhaustive-deps linter? - Stack Ove 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736701155a1948431.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
const foo = () => { return 'foo'; }
creates a new arrow function and assigns it tofoo
every time you call your hook. Why do you think it should be stable? – knittl Commented Jan 8 at 17:00useFoo
hook returns an object literal, i.e.{ foo }
, andfoo
is a new function reference each time, so it's a new object reference each time when used as a dependency for theuseEffect
hook. – Drew Reese Commented Jan 8 at 17:00