--- name: react-native-web-performance user-invocable: false description: Use when optimizing React Native Web performance. Provides patterns for code splitting, bundle optimization, memoization, and web-specific performance improvements. allowed-tools: - Read - Write - Edit - Bash - Grep - Glob --- # React Native Web - Performance Performance optimization patterns for React Native Web, focusing on bundle size, rendering performance, and web-specific optimizations. ## Key Concepts ### Code Splitting Use dynamic imports for lazy loading: ```typescript import React, { lazy, Suspense } from 'react'; import { View, ActivityIndicator } from 'react-native'; const HeavyComponent = lazy(() => import('./HeavyComponent')); function App() { return ( }> ); } ``` ### Memoization Prevent unnecessary re-renders with React.memo and hooks: ```typescript import React, { memo, useMemo, useCallback } from 'react'; interface Props { items: Item[]; onItemPress: (id: string) => void; } export const ItemList = memo(function ItemList({ items, onItemPress }: Props) { const sortedItems = useMemo( () => items.sort((a, b) => a.name.localeCompare(b.name)), [items] ); const handlePress = useCallback( (id: string) => { onItemPress(id); }, [onItemPress] ); return ( {sortedItems.map(item => ( ))} ); }); ``` ### FlatList for Large Lists Use FlatList for efficient rendering of long lists: ```typescript import { FlatList, View, Text } from 'react-native'; interface Item { id: string; title: string; } function ItemsList({ items }: { items: Item[] }) { return ( item.id} renderItem={({ item }) => ( {item.title} )} initialNumToRender={10} maxToRenderPerBatch={10} windowSize={5} removeClippedSubviews /> ); } ``` ## Best Practices ### Optimize Images ✅ Use optimized image formats and lazy loading: ```typescript import { Image } from 'react-native'; function OptimizedImage({ uri }: { uri: string }) { return ( ); } ``` ### Avoid Inline Functions ✅ Use useCallback for event handlers: ```typescript import { useCallback } from 'react'; function Component({ onSave }: { onSave: (data: Data) => void }) { const [data, setData] = useState(); const handleSave = useCallback(() => { if (data) { onSave(data); } }, [data, onSave]); return