useMemo
Learn how to memoize the results of a function between renders and only call the function again when dependencies change.
Difference between useMemo()
and useCallback()
The other Hook that’s useful for performance optimization is the useMemo()
Hook. We call it in the following way:
const memoizedValue = useMemo(valueGetterFunction, dependencyArray);
It works similarly to the useCallback()
Hook. However, it does not provide a unique identity for the function going in, but for the return value from the function, which has been passed into the useMemo()
Hook.
So, the following snippet of code:
useCallback(fn, deps);
corresponds to this snippet:
useMemo(() => fn, deps);
...