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-inline-objects.md
1---2title: Avoid Inline Objects in renderItem3impact: HIGH4impactDescription: prevents unnecessary re-renders of memoized list items5tags: lists, performance, flatlist, virtualization, memo6---78## Avoid Inline Objects in renderItem910Don't create new objects inside `renderItem` to pass as props. Inline objects11create new references on every render, breaking memoization. Pass primitive12values directly from `item` instead.1314**Incorrect (inline object breaks memoization):**1516```tsx17function UserList({ users }: { users: User[] }) {18return (19<LegendList20data={users}21renderItem={({ item }) => (22<UserRow23// Bad: new object on every render24user={{ id: item.id, name: item.name, avatar: item.avatar }}25/>26)}27/>28)29}30```3132**Incorrect (inline style object):**3334```tsx35renderItem={({ item }) => (36<UserRow37name={item.name}38// Bad: new style object on every render39style={{ backgroundColor: item.isActive ? 'green' : 'gray' }}40/>41)}42```4344**Correct (pass item directly or primitives):**4546```tsx47function UserList({ users }: { users: User[] }) {48return (49<LegendList50data={users}51renderItem={({ item }) => (52// Good: pass the item directly53<UserRow user={item} />54)}55/>56)57}58```5960**Correct (pass primitives, derive inside child):**6162```tsx63renderItem={({ item }) => (64<UserRow65id={item.id}66name={item.name}67isActive={item.isActive}68/>69)}7071const UserRow = memo(function UserRow({ id, name, isActive }: Props) {72// Good: derive style inside memoized component73const backgroundColor = isActive ? 'green' : 'gray'74return <View style={[styles.row, { backgroundColor }]}>{/* ... */}</View>75})76```7778**Correct (hoist static styles in module scope):**7980```tsx81const activeStyle = { backgroundColor: 'green' }82const inactiveStyle = { backgroundColor: 'gray' }8384renderItem={({ item }) => (85<UserRow86name={item.name}87// Good: stable references88style={item.isActive ? activeStyle : inactiveStyle}89/>90)}91```9293Passing primitives or stable references allows `memo()` to skip re-renders when94the actual values haven't changed.9596**Note:** If you have the React Compiler enabled, it handles memoization97automatically and these manual optimizations become less critical.98