0Pricing
React Native Academy · บทเรียน

ข้อมูล FlatList, renderItem และ keyExtractor

ส่งอาร์เรย์ให้พร็อพส์ data ของ FlatList เขียน renderItem เพื่อคืนคอมโพเนนต์แถวที่จัดรูปแบบ และระบุ keyExtractor เพื่อให้รายการมีเอกลักษณ์ที่คงที่

ข้อมูล FlatList, renderItem และ keyExtractor เป็นบทเรียน React Native Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน 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 ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส React Native Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส React Native Academy มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “ข้อมูล FlatList, renderItem และ keyExtractor”

ส่งอาร์เรย์ให้พร็อพส์ data ของ FlatList เขียน renderItem เพื่อคืนคอมโพเนนต์แถวที่จัดรูปแบบ และระบุ keyExtractor เพื่อให้รายการมีเอกลักษณ์ที่คงที่ คุณปฏิบัติ React Native Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน React Native Academy หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน React Native Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน

บทเรียน “ข้อมูล FlatList, renderItem และ keyExtractor” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน React Native Academy นี้ได้ไหม

ได้ บทเรียน React Native Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. ข้อมูล FlatList, renderItem และ keyExtractor
  2. การดึงเพื่อรีเฟรชและโหลดเพิ่มเติม
  3. SectionList พร้อมส่วนหัว
  4. การสร้างแอปรายการรายชื่อติดต่อ
← กลับไปที่ React Native Academy