list ยาว ๆ: สร้างแค่ที่อยู่บนจอ
จอมือถือเห็นทีละสิบกว่าแถว ดูรายชื่อหมื่นแถวเข้าไปใน ScrollView รวดเดียว แล้วเข้า FlatList ที่เก็บแค่ window รอบ ๆ จอ แล้วดูว่าการปัดเร็ว การกระโดด และ key ทำอะไรกับมันบ้าง
สิ่งที่จะได้
- เลือก ScrollView กับของไม่กี่ชิ้น และ FlatList กับ list ยาว ๆ ได้ พร้อมบอกเหตุผล
- อธิบายได้ว่า
windowSizeทำให้จำนวนแถวที่ mount คงที่ยังไง และทำไมปัดแรงแล้วเห็นแถวว่าง - ใช้
getItemLayoutให้scrollToIndexไปถึงแถวที่ 5,000 ได้โดยไม่ต้องวัดแถว - เห็นว่าทำไม key แบบ index ทำให้ติ๊กไปอยู่ผิดแถว แล้วเปลี่ยนไปใช้ id ของข้อมูล
สรุป 1 นาที
กดข้อไหน ภาพข้างล่างจะเล่นขั้นนั้นให้ดู
FlatList ที่ไม่มี getItemLayout กระโดดไปแถวที่ยังไม่ได้ render ไม่ได้ (scrollToIndex เรียก onScrollToIndexFailed)
รายชื่อหมื่นแถวใน ScrollView กับ rows.map() เปิดหน้าช้าและกินหน่วยความจำ เพราะอะไร
ในภาพคือมือถือเครื่องใหญ่ยืนอยู่ในเมืองยามค่ำ บนจอคือ list รายชื่อแบบที่คนใช้เห็นจริง (แถวที่ยัง render ไม่ทันก็คือจอว่าง ๆ) ถนนขึ้นไปคือโกดังที่เก็บข้อมูลของ list ครบหมื่นรายชื่อ หลังคาคือทุกแถวเรียงเป็นแถบเดียว แถว 1 อยู่ซ้ายสุด สีฟ้าคือแถวที่ mount อยู่ สีขาวคือตรงที่จอเห็น batch ของแถววิ่งไปหามือถือเป็นดวงไฟ ช้ากว่าของจริงให้ดูทัน จำนวนแถวที่ mount มาจากค่าเริ่มต้นของ React Native ส่วนแต่ละแถวกินหน่วยความจำเท่าไหร่แล้วแต่แถวกับมือถือ เลยไม่บอกเป็น MB
เทียบทุกแบบ: list ยาว ๆ
list หมื่นแถวเดียวกัน เปิดหน้า เลื่อนลงสามจอ แล้วสั่งกระโดดไปแถว 5,000 ดูว่าแต่ละแบบ mount ไว้กี่แถว และกระโดดได้ไหม
ใช้เมื่อ หน้าสั้น ๆ ที่มีของไม่กี่ชิ้น เช่นฟอร์มหรือหน้าตั้งค่า
ไม่ควรใช้เมื่อ ข้อมูลที่ยาวหรือไม่รู้ความยาว: ทุกแถวถูกสร้างตั้งแต่แรก
แบบจำลองตามค่าเริ่มต้นใน source ของ React Native: initialNumToRender 10, windowSize 21, batch ละ 10 แถวทุก 50 ms จำนวนแถวที่ mount คือตัวเลขของแบบจำลอง ไม่ใช่ MB ที่วัดได้ · กดช่องไหนก็ได้ ภาพใหญ่ด้านบนจะเปลี่ยนเป็นแบบนั้น พร้อมบอกว่าเหมาะกับงานแบบไหน
prop ที่สำคัญ
| prop | ค่าเริ่มต้น | ทำอะไร |
|---|---|---|
| keyExtractor | item.key ไม่มีก็ item.id ไม่มีอีกก็ index | บอกว่าแถวไหนคือข้อมูลชิ้นไหน: ใช้ id ของข้อมูล |
| initialNumToRender | 10 | แถวก่อนจอแรก |
| windowSize | 21 | จำนวนจอที่เก็บ mount: จอที่เห็น บน 10 ล่าง 10 |
| maxToRenderPerBatch | 10 | แถวที่เติมต่อ batch |
| getItemLayout | ไม่มี | offset ของทุกแถวเมื่อสูงเท่ากัน: ไม่ต้องวัด กระโดดได้ทุกที่ |
ในโค้ดจริง
// 10,000 contacts in a FlatList: only a window of rows around the screen is mounted. Stable keys, fixed-height rows
// described to the list with getItemLayout, and a row that doesn't re-render unless its own contact changes.
import { memo, useCallback, useRef } from "react";
import { FlatList, Pressable, StyleSheet, Text, View } from "react-native";
type Contact = { id: string; name: string; phone: string };
const ROW = 56; // every row is exactly this tall
const Row = memo(function Row({ item, onOpen }: { item: Contact; onOpen: (id: string) => void }) {
return (
<Pressable accessibilityRole="button" onPress={() => onOpen(item.id)} style={styles.row}>
<Text>{item.name}</Text>
<Text style={styles.sub}>{item.phone}</Text>
</Pressable>
);
});
export function Contacts({ contacts, onOpen }: { contacts: Contact[]; onOpen: (id: string) => void }) {
const list = useRef<FlatList<Contact>>(null);
// One function for the life of the list, so memo'd rows aren't re-rendered for a new prop.
const renderItem = useCallback(({ item }: { item: Contact }) => <Row item={item} onOpen={onOpen} />, [onOpen]);
return (
<View style={styles.screen}>
<Pressable accessibilityRole="button" onPress={() => list.current?.scrollToIndex({ index: Math.floor(contacts.length / 2), animated: false })}>
<Text>Jump to the middle</Text>
</Pressable>
<FlatList
ref={list}
data={contacts}
renderItem={renderItem}
// A stable id, never the index: insert one at the top and every index-keyed row gets someone else's data.
keyExtractor={(c) => c.id}
// Fixed heights: the list knows every offset, so scrollToIndex can go anywhere without measuring first.
getItemLayout={(_, index) => ({ length: ROW, offset: ROW * index, index })}
// React Native's defaults, written out: 10 rows first, 21 screens kept mounted, 10 rows a batch.
initialNumToRender={10}
windowSize={21}
maxToRenderPerBatch={10}
/>
</View>
);
}
const styles = StyleSheet.create({
screen: { flex: 1 },
row: { height: ROW, justifyContent: "center", paddingHorizontal: 16 },
sub: { color: "#6b7280" },
});
tsc เทียบกับ type ที่แต่ละ library เผยแพร่เอง (react-native และ library ที่ไฟล์นี้ import) ตรวจ type อย่างเดียว ไม่ได้รันบนมือถือผลที่พิมพ์ออกมา: tsc 6.0.3 (strict): no errors against react-native 0.88.1 (type-checked only)FlatList ที่ key คงที่ ใช้ getItemLayout กับแถวสูงเท่ากัน แถวที่ memo ไว้ renderItem ตัวเดียว และเขียนค่าเริ่มต้นของ window ไว้ให้เห็น ตรวจ type ด้วย tsc กับ type ที่ react-native 0.88 เผยแพร่เอง ไม่ได้รันบนมือถือ
คำถามที่เจอบ่อย
ScrollView กับ FlatList ใช้ต่างกันยังไง
ScrollView เหมาะกับหน้าที่มีของไม่กี่ชิ้น เพราะมันสร้างทุกอย่างทันที ข้อมูลยาวหรือไม่รู้ความยาวใช้ FlatList ที่สร้างแค่แถวรอบ ๆ จอ
FlatList mount ไว้กี่แถว
ตอนแรก initialNumToRender (10) แล้วเติมจนเต็ม window ขนาด windowSize (21) จอ คือจอที่เห็น บน 10 จอ ล่าง 10 จอ ที่เหลือไม่ได้ mount
ทำไมปัดเร็ว ๆ แล้วเห็นแถวว่าง
FlatList เติม window ทีละ batch ค่าเริ่มต้น 10 แถวทุก 50 ms ปัดเร็วกว่านั้นก็เห็นแถวที่ยัง render ไม่ทัน ทำแถวให้เบาและใช้ memo จะเติมทันขึ้น
getItemLayout ช่วยอะไร
ถ้าแถวสูงเท่ากัน บอก list ได้เลยว่าแถวที่ i อยู่ที่ length × i มันไม่ต้องวัดแถว และ scrollToIndex ไปแถวไหนก็ได้ทันที ไม่มีมัน กระโดดไปแถวที่ยังไม่ render จะเรียก onScrollToIndexFailed
ทำไม key เป็น index ถึงไม่ดี
key บอก React ว่าแถวไหนคือข้อมูลชิ้นไหน แทรกหรือลบแถวแล้ว index ของทุกแถวขยับ React ก็จับ state เดิม เช่นช่องที่ติ๊กไว้ ไปไว้กับข้อมูลอื่น ใช้ id ที่คงที่ของข้อมูลเอง
SectionList ใช้ตอนไหน
ตอนข้อมูลแบ่งเป็นกลุ่มที่มีหัวข้อ เช่นรายชื่อตามตัวอักษร หรือรายการตามวัน หัวข้อติดบนสุดเป็นค่าเริ่มต้นบน iOS และยัง virtualize เหมือน FlatList
บทต่อไป
Animation บนมือถือระดับ: กลาง
mount แค่แถวรอบ ๆ จอเป็นแล้ว ต่อไปคือเฟรม ใครคำนวณแต่ละเฟรม และทำไมเฟรมที่ JS คำนวณถึงหาย