useCallback don't magically make the function not being recreated... it just makes sure the instance created the first time is used the second time, etc... but unfortunately function creation is still done at runtime, so no performance gain from using useCallback.
So you should only use useCallback if you need a stable function instance for referencing in another component.
Since fetchData is only used internally and not exposed outside the hook, useCallback will in fact bring a memory cost and no performance gain.
Yeah, I knew the primary purpose of useCallback was for a stable reference but I also thought it was important in some cases for performance reasons. Those articles are helpful for understanding that that's not a big deal. Thanks!
Some comments have been hidden by the post's author - find out more
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
useCallback don't magically make the function not being recreated... it just makes sure the instance created the first time is used the second time, etc... but unfortunately function creation is still done at runtime, so no performance gain from using useCallback.
So you should only use useCallback if you need a stable function instance for referencing in another component.
Since fetchData is only used internally and not exposed outside the hook, useCallback will in fact bring a memory cost and no performance gain.
See this article or this one for another explanation
Really good explanation here! Even I wondered initially if useCallback will make any difference. Thanks for sharing!
Yeah, I knew the primary purpose of useCallback was for a stable reference but I also thought it was important in some cases for performance reasons. Those articles are helpful for understanding that that's not a big deal. Thanks!