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-callbacks.md
1---2title: Hoist callbacks to the root of lists3impact: MEDIUM4impactDescription: Fewer re-renders and faster lists5tags: tag1, tag26---78## List performance callbacks910**Impact: HIGH (Fewer re-renders and faster lists)**1112When passing callback functions to list items, create a single instance of the13callback at the root of the list. Items should then call it with a unique14identifier.1516**Incorrect (creates a new callback on each render):**1718```typescript19return (20<LegendList21renderItem={({ item }) => {22// bad: creates a new callback on each render23const onPress = () => handlePress(item.id)24return <Item key={item.id} item={item} onPress={onPress} />25}}26/>27)28```2930**Correct (a single function instance passed to each item):**3132```typescript33const onPress = useCallback(() => handlePress(item.id), [handlePress, item.id])3435return (36<LegendList37renderItem={({ item }) => (38<Item key={item.id} item={item} onPress={onPress} />39)}40/>41)42```4344Reference: [Link to documentation or resource](https://example.com)45