What is the purpose of the useCallback hook in React?
The useCallback hook is used to remember a function between renders. It helps avoid recreating functions unnecessarily, which can improve performance especially when passing functions to child components.
Click to reveal answer
beginner
How do you use useCallback to memoize a function that depends on a state variable count?
You wrap the function with <code>useCallback</code> and list <code>count</code> in the dependency array like this: <br><pre>const memoizedFn = useCallback(() => { console.log(count); }, [count]);</pre>
Click to reveal answer
intermediate
What happens if you pass an empty dependency array [] to useCallback?
The function is created once and never recreated again during the component's lifetime. It always uses the initial values from when it was created.
Click to reveal answer
intermediate
Why might you want to use useCallback when passing functions to child components?
Because React compares props to decide if a child should update. If you pass a new function every time, the child thinks props changed and re-renders. useCallback keeps the same function reference if dependencies don’t change, preventing unnecessary child re-renders.
Click to reveal answer
advanced
Can useCallback improve performance in all cases? Why or why not?
No. Using useCallback adds some overhead to remember the function. It helps mainly when passing functions to memoized children or expensive computations. For simple cases, it might not improve and can even slow down slightly.
Click to reveal answer
What does useCallback return?
AA memoized version of the callback function
BA new component
CA state variable
DA side effect
✗ Incorrect
useCallback returns a memoized function that only changes if its dependencies change.
Which of these is a correct way to use useCallback?
You pass a function and a dependency array to useCallback. Option D shows the correct syntax.
What happens if you omit the dependency array in useCallback?
AThe function is memoized forever
BThe function is recreated on every render
CReact throws an error
DThe function is created once and never updated
✗ Incorrect
Without dependencies, useCallback recreates the function every render, defeating its purpose.
Why is it useful to memoize functions passed as props to child components?
ATo make the function run faster
BTo increase the size of the app
CTo prevent unnecessary child re-renders
DTo change the child component's state
✗ Incorrect
Memoizing functions keeps the same reference, so children don’t re-render if props don’t change.
Which hook is often used together with useCallback to optimize child components?
AReact.memo
BuseMemo
CuseState
DuseEffect
✗ Incorrect
React.memo memoizes components and works well with useCallback to avoid unnecessary renders.
Explain how useCallback helps improve performance in React applications.
Think about how React decides to re-render children when props change.
You got /4 concepts.
Describe the correct way to use useCallback and what happens if dependencies change or stay the same.
Focus on the role of the dependency array in <code>useCallback</code>.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of the useCallback hook in React?
easy
A. To memoize a function so it doesn't get recreated on every render unless dependencies change
B. To store a value that persists between renders
C. To fetch data from an API asynchronously
D. To create a new state variable
Solution
Step 1: Understand what useCallback does
useCallback returns a memoized version of a function that only changes if its dependencies change.
Step 2: Compare options with this behavior
Only To memoize a function so it doesn't get recreated on every render unless dependencies change describes memoizing a function to avoid recreating it unnecessarily.
Final Answer:
To memoize a function so it doesn't get recreated on every render unless dependencies change -> Option A
Quick Check:
useCallback = memoize function [OK]
Hint: Remember: useCallback memoizes functions to keep them stable [OK]
Common Mistakes:
Confusing useCallback with useState
Thinking useCallback stores values instead of functions
Assuming useCallback fetches data
2. Which of the following is the correct syntax to use useCallback in a React functional component?
easy
A. const memoizedFn = useCallback(doSomething(), [dependency]);
B. const memoizedFn = useCallback(() => { doSomething(); });
C. const memoizedFn = useCallback(() => { doSomething(); }, [dependency]);
D. const memoizedFn = useCallback(doSomething, dependency);
Solution
Step 1: Recall the correct useCallback signature
The hook takes a function and an array of dependencies: useCallback(fn, [deps]).
Step 2: Analyze each option
const memoizedFn = useCallback(() => { doSomething(); }, [dependency]); correctly passes an inline function and dependencies array. const memoizedFn = useCallback(() => { doSomething(); }); misses dependencies array. const memoizedFn = useCallback(doSomething(), [dependency]); calls doSomething() immediately, passing its return value instead of the function. const memoizedFn = useCallback(doSomething, dependency); passes dependency without array brackets, invalid.
What happens when the button is clicked multiple times quickly?
medium
A. The count increments correctly each time
B. The code throws an error because of missing dependencies
C. The count increments only once and then stops
D. The count increments but may lag behind due to stale closure
Solution
Step 1: Understand the closure inside useCallback
The increment function uses count from its dependencies. Because count is in the dependency array, the function updates when count changes, but rapid clicks may use a stale value.
Step 2: Analyze behavior on quick clicks
Rapid clicks may cause the function to use an outdated count value due to closure, causing increments to lag behind actual clicks.
Final Answer:
The count increments but may lag behind due to stale closure -> Option D
Quick Check:
useCallback with state dependency can cause stale closures [OK]
Hint: State in useCallback can be stale if updated rapidly [OK]
Common Mistakes:
Assuming count always updates instantly
Ignoring closure behavior in hooks
Thinking useCallback prevents all stale data issues
4. Identify the bug in this code using useCallback:
A. Missing dependency value in the dependency array causes stale state updates
B. Using useCallback without dependencies causes infinite re-renders
C. setValue should not be called inside useCallback
D. The function should not be memoized with useCallback
Solution
Step 1: Check the dependency array of useCallback
The callback uses value but the dependency array is empty, so the function never updates with new value.
Step 2: Understand the effect of missing dependencies
Because value is missing, the callback uses the initial value forever, causing stale increments.
Final Answer:
Missing dependency value in the dependency array causes stale state updates -> Option A
Quick Check:
Dependencies must include all used variables [OK]
Hint: Always include all variables used inside useCallback in dependencies [OK]
Common Mistakes:
Leaving dependency array empty when variables are used
Thinking setState functions need dependencies
Confusing useCallback with useMemo
5. You want to optimize a child component that receives a callback prop to avoid unnecessary re-renders. Which approach correctly uses useCallback to achieve this?
A. const handleClick = React.useCallback(() => setCount(count + 1), [count]);
B. const handleClick = React.useCallback(() => setCount(c => c + 1), []);
C. const handleClick = () => setCount(count + 1);
D. const handleClick = React.useCallback(() => setCount(count + 1));
Solution
Step 1: Understand the goal to avoid unnecessary re-renders
We want handleClick to be stable and not change unless needed, so the child doesn't re-render unnecessarily.
Step 2: Analyze each option's dependency array and function
const handleClick = React.useCallback(() => setCount(count + 1), [count]); depends on count, so it changes every time count changes, causing re-renders. const handleClick = React.useCallback(() => setCount(c => c + 1), []); uses functional update c => c + 1 and empty dependencies, so handleClick is stable. const handleClick = () => setCount(count + 1); recreates function every render. const handleClick = React.useCallback(() => setCount(count + 1)); misses dependencies array, which is invalid.
Final Answer:
const handleClick = React.useCallback(() => setCount(c => c + 1), []); -> Option B
Quick Check:
Stable callback with functional update and empty deps = const handleClick = React.useCallback(() => setCount(c => c + 1), []); [OK]
Hint: Use functional updates with empty deps to keep callback stable [OK]
Common Mistakes:
Including state in dependencies causing new functions each render
Not using functional updates when deps are empty
Omitting dependency array causing bugs
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.