FlatList 数据、renderItem 与 keyExtractor
将数组传给 FlatList 的 data 属性,实现 renderItem 以返回带样式的行组件,并提供 keyExtractor 以稳定地标识列表项。
FlatList 数据、renderItem 与 keyExtractor 是 CoddyKit 上的免费 React Native Academy 课时。 这是第 1 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 React Native Academy 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 React Native Academy 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
Why FlatList Instead of ScrollView?
ScrollView renders all its children at once, which becomes slow when displaying hundreds of items. FlatList virtualizes the list — it only renders the items currently visible on screen plus a small buffer, dramatically reducing memory usage and improving scroll performance for large datasets.
The data Prop
Pass your array of items to the data prop. FlatList iterates over this array and calls renderItem for each element. The array can contain any kind of JavaScript objects. Changes to the data array trigger a re-render of affected list items.
const DATA = [
{ id: '1', name: 'Alice' },
{ id: '2', name: 'Bob' },
{ id: '3', name: 'Carol' },
];
<FlatList data={DATA} ... />The renderItem Prop
The renderItem prop is a function that receives an object with an item property (the current array element) and must return a React element. This is where you define how each row looks. Keep it as a stable reference using useCallback to avoid unnecessary re-renders.
const renderItem = ({ item }) => (
<View style={styles.row}>
<Text>{item.name}</Text>
</View>
);
<FlatList
data={DATA}
renderItem={renderItem}
/>The keyExtractor Prop
keyExtractor tells FlatList how to extract a unique string key from each item. React uses these keys to track which items changed during re-renders. Without a proper key extractor FlatList falls back to the array index, which can cause subtle bugs during data mutations.
<FlatList
data={DATA}
renderItem={renderItem}
keyExtractor={(item) => item.id}
/>A Complete FlatList Example
Combining data, renderItem, and keyExtractor gives you a fully functional, scrollable, virtualized list. You can style the rows inside renderItem just like any other component. FlatList automatically handles scroll and touch events.
import { FlatList, View, Text, StyleSheet } from 'react-native';
const DATA = [
{ id: '1', title: 'First Item' },
{ id: '2', title: 'Second Item' },
{ id: '3', title: 'Third Item' },
];
export default function App() {
return (
<FlatList
data={DATA}
keyExtractor={(item) => item.id}
renderItem={({ item }) => (
<View style={styles.item}>
<Text>{item.title}</Text>
</View>
)}
/>
);
}
const styles = StyleSheet.create({
item: { padding: 16, borderBottomWidth: 1, borderColor: '#eee' },
});Adding a List Header and Footer
Use ListHeaderComponent to render content above the list (such as a search bar or page title) and ListFooterComponent for content below (such as a loading spinner when fetching more data). These components scroll with the list, unlike a view placed outside FlatList.
<FlatList
data={DATA}
renderItem={renderItem}
keyExtractor={(item) => item.id}
ListHeaderComponent={() => <Text style={styles.header}>Users</Text>}
ListFooterComponent={() => <ActivityIndicator />}
/>Empty List with ListEmptyComponent
When the data array is empty, FlatList renders nothing by default. Provide a ListEmptyComponent to display a placeholder message or illustration when there are no items. This is much cleaner than wrapping FlatList in a conditional.
<FlatList
data={filteredData}
renderItem={renderItem}
keyExtractor={(item) => item.id}
ListEmptyComponent={() => (
<Text style={styles.empty}>No results found</Text>
)}
/>Item Separators with ItemSeparatorComponent
Instead of adding border styles directly to item rows, use ItemSeparatorComponent to render a separator between items. React Native only renders the separator between items, not before the first or after the last, which avoids double borders at the edges.
const Separator = () => (
<View style={{ height: 1, backgroundColor: '#eee' }} />
);
<FlatList
data={DATA}
renderItem={renderItem}
keyExtractor={(item) => item.id}
ItemSeparatorComponent={Separator}
/>Horizontal FlatList
Set the horizontal prop to true to render a horizontally scrolling list. This is useful for carousels, category chips, or story rows. The showsHorizontalScrollIndicator prop can hide the scroll bar for a cleaner look.
<FlatList
horizontal
showsHorizontalScrollIndicator={false}
data={categories}
renderItem={({ item }) => (
<View style={styles.chip}>
<Text>{item.label}</Text>
</View>
)}
keyExtractor={(item) => item.id}
/>Scrolling to an Index Programmatically
Use the ref on FlatList and call flatListRef.current.scrollToIndex({ index, animated: true }) to jump to a specific item. You can also use scrollToOffset for pixel-level control or scrollToEnd to scroll to the bottom of the list.
const flatListRef = useRef(null);
const scrollToTop = () => {
flatListRef.current.scrollToIndex({ index: 0, animated: true });
};
<FlatList ref={flatListRef} data={DATA} renderItem={renderItem} />Avoiding Common FlatList Mistakes
Avoid defining renderItem as an anonymous arrow function directly in JSX (renderItem={() => ...}). This creates a new function reference on every render, causing every row to re-render unnecessarily. Define it outside the JSX or memoize it with useCallback. Also never use an array index as the key — always use a stable unique ID.
// Bad: anonymous function in JSX
<FlatList renderItem={({ item }) => <Row item={item} />} />
// Good: stable reference
const renderItem = useCallback(({ item }) => <Row item={item} />, []);
<FlatList renderItem={renderItem} />Quick Check
Test your understanding of FlatList data, renderItem, and keyExtractor from this lesson.
Lesson Recap
In this lesson you learned: FlatList virtualizes large lists by only rendering visible items, renderItem defines the appearance of each row, and keyExtractor provides stable unique keys for efficient React reconciliation. Next up we explore pull-to-refresh and infinite scroll with load more.
常见问题解答
「FlatList 数据、renderItem 与 keyExtractor」课时是免费的吗?
是的 — 「FlatList 数据、renderItem 与 keyExtractor」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 React Native Academy 课程的其余内容,请升级到 CoddyKit PRO。 React Native Academy 课程共包含 4 节课。
「FlatList 数据、renderItem 与 keyExtractor」这节课中我会学到什么?
将数组传给 FlatList 的 data 属性,实现 renderItem 以返回带样式的行组件,并提供 keyExtractor 以稳定地标识列表项。 你通过在浏览器中直接运行的动手代码来练习 React Native Academy,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 React Native Academy 需要有经验吗?
无需任何先前经验。CoddyKit 上的 React Native Academy 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 1 节课,共 4 节。
「FlatList 数据、renderItem 与 keyExtractor」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 React Native Academy 课中编写并运行代码吗?
能。每节 React Native Academy 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。
此课程中的所有课时
- FlatList 数据、renderItem 与 keyExtractor
- 下拉刷新与加载更多
- 带标题的 SectionList
- 构建联系人列表应用