Performance
Understand the idea
React is often fast enough without extra caching. useMemo and useCallback help in specific cases — and hurt when you add them by habit.
What these two hooks do
useMemo(function, dependencies) runs the function when a dependency changes, and otherwise returns the last result.
useCallback(function, dependencies) returns the same function instance until a dependency changes.
Both are : remember work so you can skip repeating it.
They do not make slow code magically fast by themselves. They help when:
- a calculation is genuinely expensive, or
- a child is wrapped in React.memo and would re-render because you pass a new function every time.
When it is worth it
| Situation | Reach for | Leave it alone |
|---|---|---|
| Filtering a huge list on every keystroke | useMemo | A list of three items |
| Passing a function into a memoized child | useCallback | A button handler in the same component |
| Building an object an effect depends on | Often depend on an id instead | Caching the object “just in case” |
If you have not seen a slowdown, and no child is memoized, write the plain version.
Sometimes the real fix is the dependency
People add useMemo because an effect re-runs too often. Often the better fix is: depend on user.id, not on a whole user object created inline during render. New object every render means “changed” every render.
How to read the live example
- count starts at 1.
- total is count * 2 inside useMemo, so it recalculates when count changes.
- bump is a useCallback that increases count.
- The console names the lesson rule: memo, callback, skip, measure, not-every-function.
Click the button. total should update. That shows the tools working - not that every file needs them.
Default to the simple code
In review, ask what got faster. If the answer is only “I wrapped it,” ask for a measurement or a memoized child that needed a stable function. A is not automatically a problem. A wrong dependency is.
Mental model
FOLLOW THE VALUE
Dependencies are the question.
01 / 03Trace the transformation
Live example
The example shows a memoized total and a stable bump , then logs the rule: measure first, and do not wrap every .
import { useMemo, useCallback, useState } from "react"; export default function App() { const [count, setCount] = useState(1); // Input the memo depends on. const total = useMemo(() => count * 2, [count]); // Cache until count changes. const bump = useCallback(() => setCount((value) => value + 1), []); // Stable function identity. console.log("memo"); // Cached calculation. console.log("callback"); // Cached function. console.log("skip"); // Skip work when inputs are unchanged. console.log("measure"); // Measure before you memoize. console.log("not-every-function"); // Do not wrap every handler. return <button type="button" onClick={bump}>{total}</button>; }
Guided challenge
Do not memoize by habit
The last log says always. Change it to not-every-function.
export default function App() { console.log("memo"); // Keep this log. console.log("callback"); // Keep this log. console.log("skip"); // Keep this log. console.log("measure"); // Keep this log. console.log("always"); // Wrong - change to not-every-function. return <p>always</p>; }
EXPECTED CONSOLE OUTPUT
memo callback skip measure not-every-function
Hints 0/2
Free sandbox
Make a variation, test an assumption, or break the example on purpose. This draft is saved separately.
import { useState } from "react"; export default function App() { const [count, setCount] = useState(0); // Plain state is often enough. return <button type="button" onClick={() => setCount(count + 1)}>{count}</button>; // No memo needed here. }
Quiz
A quick check for understanding. Retry as often as you like; your learning path stays open.
Ready to call this one understood?
Mark this lesson complete to earn 200 XP. You can always revisit it.