Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Build performant React Native and Expo apps with best practices for lists, animations, and navigation
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
rules/list-performance-item-memo.md
1---2title: Pass Primitives to List Items for Memoization3impact: HIGH4impactDescription: enables effective memo() comparison5tags: lists, performance, memo, primitives6---78## Pass Primitives to List Items for Memoization910When possible, pass only primitive values (strings, numbers, booleans) as props11to list item components. Primitives enable shallow comparison in `memo()` to12work correctly, skipping re-renders when values haven't changed.1314**Incorrect (object prop requires deep comparison):**1516```tsx17type User = { id: string; name: string; email: string; avatar: string }1819const UserRow = memo(function UserRow({ user }: { user: User }) {20// memo() compares user by reference, not value21// If parent creates new user object, this re-renders even if data is same22return <Text>{user.name}</Text>23})2425renderItem={({ item }) => <UserRow user={item} />}26```2728This can still be optimized, but it is harder to memoize properly.2930**Correct (primitive props enable shallow comparison):**3132```tsx33const UserRow = memo(function UserRow({34id,35name,36email,37}: {38id: string39name: string40email: string41}) {42// memo() compares each primitive directly43// Re-renders only if id, name, or email actually changed44return <Text>{name}</Text>45})4647renderItem={({ item }) => (48<UserRow id={item.id} name={item.name} email={item.email} />49)}50```5152**Pass only what you need:**5354```tsx55// Incorrect: passing entire item when you only need name56<UserRow user={item} />5758// Correct: pass only the fields the component uses59<UserRow name={item.name} avatarUrl={item.avatar} />60```6162**For callbacks, hoist or use item ID:**6364```tsx65// Incorrect: inline function creates new reference66<UserRow name={item.name} onPress={() => handlePress(item.id)} />6768// Correct: pass ID, handle in child69<UserRow id={item.id} name={item.name} />7071const UserRow = memo(function UserRow({ id, name }: Props) {72const handlePress = useCallback(() => {73// use id here74}, [id])75return <Pressable onPress={handlePress}><Text>{name}</Text></Pressable>76})77```7879Primitive props make memoization predictable and effective.8081**Note:** If you have the React Compiler enabled, you do not need to use82`memo()` or `useCallback()`, but the object references still apply.83