ReactPart 4 of React Mastery✦ Featured
React Hooks You're Probably Using Wrong
Deep dive into useCallback, useMemo, and useEffect — with real benchmarks showing when they help and when they hurt.
R
by RupaThe Problem With Premature Optimization
I see this pattern constantly in code reviews:
// Don't do this blindly
const handleClick = useCallback(() => {
doSomething(value);
}, [value]);
const computed = useMemo(() => {
return items.map(i => i.name);
}, [items]);
Both of these might actually be slowing your app down.
When useCallback Actually Hurts
useCallback has a cost — it stores the function in memory and runs a dependency comparison on every render.
If the component re-renders anyway, you've paid the cost of useCallback for zero benefit.
ℹThe rule
Only use useCallback when passing callbacks to components wrapped in React.memo, or as dependencies to other hooks.
useEffect — The Most Misused Hook
The most common mistake:
// ❌ Wrong — creates an infinite loop
useEffect(() => {
fetchUser().then(setUser);
}, [fetchUser]); // fetchUser recreated every render
// ✅ Right — fetch on mount only
useEffect(() => {
fetchUser().then(setUser);
}, []); // eslint-disable-line react-hooks/exhaustive-deps
Try It Yourself
hooks-demo.js
Loading editor...
The Decision Framework
Use useMemo when:
- The computation takes > 1ms (measure with
console.time) - The result is used in multiple places
- The component renders frequently
Skip useMemo when:
- It's a simple transformation like
.filter()on a small array - The component rarely re-renders
#react#typescript#performance#hooks
✦ Enjoyed this post?
Get posts like this in your inbox
No spam, just real tutorials when they're ready.
Discussion
Powered by GitHubComments use GitHub Discussions — no separate account needed if you have GitHub.