0
0
Reactframework~5 mins

useCallback hook in React - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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(() =&gt; { 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
Which of these is a correct way to use useCallback?
AuseCallback(() =&gt; { doSomething(); })
BuseCallback(doSomething(), [])
CuseCallback(doSomething)
DuseCallback(() =&gt; { doSomething(); }, [dependency])
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
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
Which hook is often used together with useCallback to optimize child components?
AReact.memo
BuseMemo
CuseState
DuseEffect
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.