--- name: react-native-performance user-invocable: false description: Use when optimizing React Native app performance. Covers FlatList optimization, memoization, image optimization, bundle size reduction, and profiling techniques. allowed-tools: - Read - Write - Edit - Bash - Grep - Glob --- # React Native Performance Use this skill when optimizing React Native applications for better performance, faster load times, and smoother user experiences. ## Key Concepts ### List Performance Optimize FlatList for large datasets: ```tsx import React, { useCallback } from 'react'; import { FlatList, View, Text, StyleSheet } from 'react-native'; interface Item { id: string; title: string; } const ItemComponent = React.memo(({ item }: { item: Item }) => ( {item.title} )); function OptimizedList({ data }: { data: Item[] }) { const renderItem = useCallback( ({ item }: { item: Item }) => , [] ); const keyExtractor = useCallback((item: Item) => item.id, []); const getItemLayout = useCallback( (data: any, index: number) => ({ length: 80, // Fixed item height offset: 80 * index, index, }), [] ); return ( ); } const styles = StyleSheet.create({ item: { height: 80, padding: 16, borderBottomWidth: 1, borderBottomColor: '#eee', }, }); ``` ### Component Memoization Use React.memo and useMemo: ```tsx import React, { useMemo } from 'react'; import { View, Text } from 'react-native'; interface UserCardProps { user: { id: string; name: string; email: string; }; onPress: () => void; } // Memoize component to prevent unnecessary re-renders const UserCard = React.memo(({ user, onPress }: UserCardProps) => { return ( {user.name} {user.email} ); }); // Memoize expensive computations function UserList({ users }: { users: User[] }) { const sortedUsers = useMemo(() => { return [...users].sort((a, b) => a.name.localeCompare(b.name)); }, [users]); return ( {sortedUsers.map(user => ( {}} /> ))} ); } ``` ### Image Optimization Optimize image loading and caching: ```tsx import React from 'react'; import { Image, View } from 'react-native'; import FastImage from 'react-native-fast-image'; // Use FastImage for better performance function OptimizedImage({ uri }: { uri: string }) { return ( ); } // Lazy load images function LazyImage({ uri }: { uri: string }) { return ( ); } ``` ## Best Practices ### Use useCallback for Event Handlers Prevent unnecessary re-renders: ```tsx import React, { useCallback, useState } from 'react'; import { View, Button, Text } from 'react-native'; function Counter() { const [count, setCount] = useState(0); // Bad - Creates new function on every render // const increment = () => setCount(count + 1); // Good - Memoized callback const increment = useCallback(() => { setCount(c => c + 1); }, []); return ( {count}