0
0
Reactframework~10 mins

useCallback hook in React - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - useCallback hook
Component renders
useCallback creates memoized function
Function passed to child or used
State or props change?
NoReturn memoized function
Yes
Dependencies changed?
NoReturn memoized function
Yes
Create new function instance
Component re-renders with new function
When a component renders, useCallback returns a memoized function that only changes if its dependencies change, avoiding unnecessary re-creations.
Execution Sample
React
const memoizedCallback = useCallback(() => {
  console.log(count);
}, [count]);
This code creates a function that logs 'count' and only changes when 'count' changes.
Execution Table
StepRender CountDependencies [count]ActionFunction Instance Changed?Output
110Initial render, create functionYesFunction created, logs 0 when called
220Re-render, dependencies sameNoSame function instance reused
331Re-render, dependencies changedYesNew function created, logs 1 when called
441Re-render, dependencies sameNoSame function instance reused
552Re-render, dependencies changedYesNew function created, logs 2 when called
662Re-render, dependencies sameNoSame function instance reused
772No re-render, no actionNoFunction unchanged
💡 Component stops re-rendering or dependencies stop changing, so function instance remains stable.
Variable Tracker
VariableStartAfter Render 1After Render 2After Render 3After Render 4After Render 5After Render 6Final
count00011222
memoizedCallbackundefinedfunc#1func#1func#2func#2func#3func#3func#3
Key Moments - 3 Insights
Why does the function instance not change on every render?
Because useCallback returns the same function instance if dependencies haven't changed, as shown in rows 2, 4, and 6 where 'Function Instance Changed?' is 'No'.
What triggers useCallback to create a new function?
When a dependency like 'count' changes value, useCallback creates a new function instance, as seen in rows 3, 5 where dependencies changed.
Does useCallback prevent component re-renders?
No, useCallback only memoizes the function instance; component re-renders depend on state or props changes, not shown directly here.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, at which render does the function instance first change?
ARender 2
BRender 3
CRender 1
DRender 4
💡 Hint
Check the 'Function Instance Changed?' column for the first 'Yes' value.
At which render does the dependency 'count' change from 0 to 1?
ARender 2
BRender 3
CRender 4
DRender 5
💡 Hint
Look at the 'Dependencies [count]' column for the change from 0 to 1.
If the dependency array was empty, how would the function instance behave across renders?
AIt would never change after initial render
BIt would change only on odd renders
CIt would change every render
DIt would change only when count changes
💡 Hint
An empty dependency array means the function is created once and never recreated.
Concept Snapshot
useCallback(() => { /* function */ }, [deps])
- Returns memoized function
- Function changes only if dependencies change
- Helps avoid unnecessary function re-creations
- Useful when passing functions to child components
- Does not prevent component re-renders
Full Transcript
The useCallback hook in React returns a memoized function that only changes if its dependencies change. On the first render, it creates a new function. On subsequent renders, if dependencies are unchanged, it returns the same function instance. When dependencies change, it creates a new function instance. This helps optimize performance by preventing unnecessary function recreations, especially when passing functions to child components. However, useCallback does not stop the component from re-rendering; it only stabilizes the function reference.