0

I am studying React, and I am stuck on understanding the benefits of useCallback hook. According to this website,

In function components, the execution of the whole function is the equivalent of the render function in class components.

If in the functional components, the whole function is executed on every render, doesn't that mean that the whole code would be re-executed afresh. As a result the useCallback hook would start from a clean state on each render, meaning that useCallback cache would be reinitialized and all previous cache entries would be lost?

Bawantha
  • 3,644
  • 4
  • 24
  • 36
A Singh
  • 107
  • 7
  • Show how you are trying to use `useCallback`. The whole goal of it is memoization so that you don't have to reconstruct the same thing over and over. You may or may not be doing so based on your implementation. See also https://stackoverflow.com/q/54963248/691711 – zero298 Apr 19 '21 at 17:41
  • Check this [link](https://kentcdodds.com/blog/usememo-and-usecallback). – Medi Apr 19 '21 at 17:41
  • All *your* code is run fresh, but react saves values between renders, if you ask it to. useCallback is one way to ask it to. – Nicholas Tower Apr 19 '21 at 17:42

0 Answers0