Lesson 4 of 9
useMemo & useCallback - What Actually Helps
useMemo and useCallback are tools to prevent expensive recomputation and unstable references - but they add overhead of their own. Knowing when they help vs when they're premature optimization is a senior skill.
When to Memoize
import { useMemo, useCallback } from 'react';
// ---- useMemo - expensive computation ----
// ✅ Good use: filtering/sorting 1000+ items
function FilteredFeed({ posts, filter }: { posts: Post[]; filter: string }) {
const filtered = useMemo(() => {
return posts
.filter(p => p.category === filter || filter === 'all')
.sort((a, b) => new Date(b.date).getTime() - new Date(a.date).getTime());
}, [posts, filter]); // recalculate only when these change
return <FlatList data={filtered} renderItem={({ item }) => <PostCard post={item} />} keyExtractor={p => p.id} />;
}
// ❌ Premature: simple data
// const doubled = useMemo(() => count * 2, [count]); // just write count * 2
// ---- useCallback - stable function reference ----
// ✅ Critical: renderItem and keyExtractor in FlatList
function PostFeed({ posts }: { posts: Post[] }) {
const keyExtractor = useCallback((item: Post) => item.id, []);
const renderItem = useCallback(({ item }: { item: Post }) => (
<PostCard post={item} />
), []); // no deps = never recreated (PostCard must be memo'd)
return (
<FlatList
data={posts}
keyExtractor={keyExtractor} // stable reference → no FlatList re-render
renderItem={renderItem} // stable reference
/>
);
}
// ✅ Good: event handler passed to React.memo child
const MemoizedChild = React.memo(({ onPress }: { onPress: () => void }) => (
<Pressable onPress={onPress}><Text>Press me</Text></Pressable>
));
function Parent({ itemId }: { itemId: string }) {
// Without useCallback: new function every render → MemoizedChild always re-renders
const handlePress = useCallback(() => {
doSomethingWith(itemId);
}, [itemId]); // only new function if itemId changes
return <MemoizedChild onPress={handlePress} />;
}
interface Post { id: string; category: string; date: string; }
function PostCard({ post }: { post: Post }) { return null; }
function doSomethingWith(id: string) {}Tip
Tip
Practice useMemo useCallback What Actually Helps in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.
Preventing unnecessary re-renders in FlatList items and heavy calculations
Practice Task
Note
Practice Task - (1) Write a working example of useMemo useCallback What Actually Helps from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.
Quick Quiz
Common Mistake
Warning
A common mistake with useMemo useCallback What Actually Helps is skipping edge case testing - empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready react native code.
Finished this lesson?