Recall & Review
beginner
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?✗ Incorrect
useCallback returns a memoized function that only changes if its dependencies change.Which of these is a correct way to use
useCallback?✗ Incorrect
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?✗ 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?
✗ 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?✗ 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.