What is useCallBack in React?
The useCallBack hook helps us to memoize the functions so that it prevents the re-creating of functions on every re-render.
The function passed to the useCallBack hook is only recreated when one of it’s dependencies are changed.
When to use useCallBack?
Consider this scenario where you have to pass a callback function to the child. In functional components if you pass a callback function to child, whenever parent gets re-rendered, child also gets re-rendered, even when props doesn’t change. To avoid this scenario, the useCallBack hook gives you a referential function that is same on multiple renders
Referential Equality
When you pass function as prop to child component,
When not to use useCallBack?
Performance optimizations are not free. They ALWAYS come with a cost but do NOT always come with a benefit to offset that cost.
tags: basics , react , hooks sources: - When to useMemo and useCallback