# List Optimization ## Optimized FlatList ```typescript import { FlatList, ListRenderItem } from 'react-native'; import { memo, useCallback } from 'react'; interface Item { id: string; title: string; subtitle: string; } // Memoized list item const ListItem = memo(function ListItem({ item, onPress }: { item: Item; onPress: (id: string) => void; }) { return ( onPress(item.id)} style={styles.item}> {item.title} {item.subtitle} ); }); function OptimizedList({ data }: { data: Item[] }) { // Memoize callbacks const handlePress = useCallback((id: string) => { console.log('Selected:', id); }, []); const renderItem: ListRenderItem = useCallback( ({ item }) => , [handlePress] ); const keyExtractor = useCallback((item: Item) => item.id, []); // Fixed height for getItemLayout const getItemLayout = useCallback( (_: any, index: number) => ({ length: ITEM_HEIGHT, offset: ITEM_HEIGHT * index, index, }), [] ); return ( ); } const ITEM_HEIGHT = 72; ``` ## SectionList ```typescript import { SectionList } from 'react-native'; interface Section { title: string; data: Item[]; } function GroupedList({ sections }: { sections: Section[] }) { const renderSectionHeader = useCallback( ({ section }: { section: Section }) => ( {section.title} ), [] ); return ( ); } ``` ## Pull to Refresh ```typescript function RefreshableList({ data, onRefresh }: Props) { const [refreshing, setRefreshing] = useState(false); const handleRefresh = useCallback(async () => { setRefreshing(true); await onRefresh(); setRefreshing(false); }, [onRefresh]); return ( } /> ); } ``` ## Infinite Scroll ```typescript function InfiniteList() { const [data, setData] = useState([]); const [loading, setLoading] = useState(false); const [hasMore, setHasMore] = useState(true); const loadMore = useCallback(async () => { if (loading || !hasMore) return; setLoading(true); const newItems = await fetchMoreItems(data.length); if (newItems.length === 0) { setHasMore(false); } else { setData(prev => [...prev, ...newItems]); } setLoading(false); }, [data.length, loading, hasMore]); const renderFooter = useCallback(() => { if (!loading) return null; return ; }, [loading]); return ( ); } ``` ## FlashList (Alternative) ```typescript import { FlashList } from '@shopify/flash-list'; function FastList({ data }: { data: Item[] }) { return ( ); } ``` ## Quick Reference | Prop | Purpose | |------|---------| | `removeClippedSubviews` | Unmount off-screen items | | `maxToRenderPerBatch` | Items per render batch | | `windowSize` | Render window multiplier | | `initialNumToRender` | Initial items to render | | `getItemLayout` | Skip measurement (fixed height) | | Optimization | When | |--------------|------| | `memo()` | All list items | | `useCallback` | renderItem, keyExtractor | | `getItemLayout` | Fixed height items | | `FlashList` | Very large lists |