React's useCallback
hook offers a performance gain when passing the generated (memoized) function to a child component in order to avoid unnecessary re-renders.
Do I get any performance gain when the hook is used as internal ("private") function? For example:
function Foo({numbers}) {
const dep1 = React.useMemo(() => {......}, [....])
const calc = React.useCallback((a) => a * dep1, [dep1])
return (
<>
{numbers.map((num) => (
<div key={num}>
Num: {num}, Calculated: {calc(num)}
</div>
))}
</>
);
}
or does a simple
const calc = (a) => a * dep1
is the same for that case?
In other words, since useCallback
is memoizing the function reference, not the function itself, do I have to use it when not passing it as a prop?