Because I use fetchPosts which is a dependency in my useEffect. But fetchPosts will never updated (it's a useCallback with []as dependency) so useEffect will not be executed a second time.
Right, I understand why it works, but why pick one over the other? Would it be faster to not wrap in useCalllback and use []? This is a fetch, so I guess any savings would be washed away in network.
I want to have a function for getting my data. Maybe in the future I would like to have a button to "refresh" my data. And call "fetchPosts" by clicking on it. Or maybe, for my future pagination by adding page as a new parameter of my function fetchPosts.
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.
In the final example, why use
[fetchPosts]over[]?Because I use
fetchPostswhich is a dependency in my useEffect. ButfetchPostswill never updated (it's auseCallbackwith[]as dependency) so useEffect will not be executed a second time.Right, I understand why it works, but why pick one over the other? Would it be faster to not wrap in useCalllback and use
[]? This is a fetch, so I guess any savings would be washed away in network.I want to have a function for getting my data. Maybe in the future I would like to have a button to "refresh" my data. And call "fetchPosts" by clicking on it. Or maybe, for my future pagination by adding
pageas a new parameter of my functionfetchPosts.