In this example, the square of the number is only recalculated when number
changes, avoiding unnecessary computations.
useCallback
is used to memoize functions so that they are not re-created on every render unless one of the dependencies changes. This is particularly useful when passing callbacks to child components that rely on referential equality to avoid unnecessary renders.