0
0
Reactframework~5 mins

useMemo hook in React - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is the purpose of the useMemo hook in React?
The useMemo hook helps to remember (or memoize) a value so React does not have to recalculate it on every render, improving performance.
Click to reveal answer
beginner
How do you use useMemo to optimize a slow calculation?
Wrap the slow calculation inside useMemo(() => slowCalculation(), [dependencies]). React will only recalculate when dependencies change.
Click to reveal answer
intermediate
What happens if you pass an empty dependency array [] to useMemo?
The memoized value is calculated once on the first render and never recalculated again, unless the component unmounts and remounts.
Click to reveal answer
intermediate
Can useMemo replace useState or useEffect?
No. useMemo only memoizes values. useState manages state, and useEffect runs side effects. They serve different purposes.
Click to reveal answer
intermediate
Why should you not overuse useMemo?
Because memoization itself has a cost. Use it only when a calculation is expensive and the saved work improves performance noticeably.
Click to reveal answer
What does useMemo return?
AA side effect callback
BA function to update state
CA memoized value
DA component
When does useMemo recalculate the memoized value?
AEvery render
BOnly when dependencies change
COnly on first render
DNever
Which of these is a good use case for useMemo?
AMemoizing a heavy calculation based on props
BRunning an API call
CReplacing <code>useState</code> for state management
DMemoizing a simple string constant
What happens if you omit the dependency array in useMemo?
AMemoization is disabled; recalculates every render
BMemoizes forever
CCauses an error
DMemoizes only once
Which hook is best to run side effects, not memoize values?
AuseCallback
BuseMemo
CuseState
DuseEffect
Explain how useMemo helps improve React app performance.
Think about when React re-renders and how <code>useMemo</code> saves work.
You got /4 concepts.
    Describe the difference between useMemo and useEffect.
    One remembers values, the other runs code after render.
    You got /4 concepts.