DEV Community

Discussion on: useAxios : A simple custom hook for calling APIs using axios

Collapse
 
ecyrbe profile image
ecyrbe • Edited

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

Collapse
 
hey_yogini profile image
Yogini Bende

Really good explanation here! Even I wondered initially if useCallback will make any difference. Thanks for sharing!

Collapse
 
embrycode profile image
Mason Embry

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