Lesson 6 of 9
useCallback + FlatList - Preventing Re-renders
FlatList items re-rendering is the most common performance problem in React Native list screens. The fix requires three things working together: React.memo on the item component, useCallback for renderItem and keyExtractor, and stable props passed to list items.
The Complete FlatList Memoization Pattern
import { useCallback, useMemo } from 'react';
import { FlatList, View, Text, Pressable, StyleSheet } from 'react-native';
interface Todo { id: string; text: string; done: boolean; }
interface TodoItemProps { todo: Todo; onToggle: (id: string) => void; onDelete: (id: string) => void; }
// 1️⃣ Memoize the item component - won't re-render unless props change
const TodoItem = React.memo(function TodoItem({ todo, onToggle, onDelete }: TodoItemProps) {
console.log('Render:', todo.id); // uncomment during debugging to count renders
return (
<View style={styles.item}>
<Pressable onPress={() => onToggle(todo.id)} style={styles.checkbox}>
{todo.done && <Text style={styles.check}>✓</Text>}
</Pressable>
<Text style={[styles.text, todo.done && styles.done]}>{todo.text}</Text>
<Pressable onPress={() => onDelete(todo.id)} accessibilityLabel={`Delete ${todo.text}`}>
<Text style={styles.delete}>🗑</Text>
</Pressable>
</View>
);
});
function TodoList({ todos, onToggle, onDelete }: { todos: Todo[]; onToggle: (id: string) => void; onDelete: (id: string) => void }) {
// 2️⃣ Memoize keyExtractor - prevents FlatList re-render when parent re-renders
const keyExtractor = useCallback((item: Todo) => item.id, []);
// 3️⃣ Memoize renderItem - prevent new function reference every render
// Note: onToggle and onDelete must be stable (useCallback from parent)
const renderItem = useCallback(({ item }: { item: Todo }) => (
<TodoItem todo={item} onToggle={onToggle} onDelete={onDelete} />
), [onToggle, onDelete]);
return (
<FlatList
data={todos}
keyExtractor={keyExtractor}
renderItem={renderItem}
extraData={todos} // re-render FlatList when todos array changes
/>
);
}
const styles = StyleSheet.create({
item: { flexDirection: 'row', alignItems: 'center', gap: 12, padding: 14, backgroundColor: '#1e293b', borderRadius: 10, marginBottom: 8 },
checkbox: { width: 22, height: 22, borderRadius: 6, borderWidth: 2, borderColor: '#229c6b', alignItems: 'center', justifyContent: 'center' },
check: { color: '#229c6b', fontWeight: '700', fontSize: 13 },
text: { flex: 1, fontSize: 15, color: '#f1f5f9' },
done: { textDecorationLine: 'line-through', color: '#475569' },
delete: { fontSize: 18 },
});Tip
Tip
Practice useCallback FlatList Preventing Rerenders in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.
Optimizing renderItem and keyExtractor to maintain constant 60 FPS scrolling
Practice Task
Note
Practice Task - (1) Write a working example of useCallback FlatList Preventing Rerenders 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 useCallback FlatList Preventing Rerenders 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?