The useCallback Hook in React returns a memoized version of a callback function.
Think of memoization as caching a value to avoid recalculating it. |
This approach helps isolate resource-intensive functions, preventing them from running on every render. The useCallback
Hook runs only when one of its dependencies updates, which can enhance performance.
The useCallback and useMemo Hooks are similar, with the key difference being that useMemo returns a memoized value, while useCallback returns a memoized function. You can explore useMemo further in its dedicated chapter. |
Using useCallback
helps prevent unnecessary re-renders of a component, ensuring it only re-renders when its props change, such as when todos
change.
This example is similar to the one discussed in the React.memo section.
index.js
import |
Todos.js
import |
Try running this and clicking the count increment button.
The Todos component re-renders despite no changes to the todos because, due to “referential equality,” the addTodo function is recreated on each render, making it seem like it has changed.