ရိုးရိုးလေး ရှင်းပြမယ်
Lists with FlatList ကို ရိုးရိုးလေးနားလည်မယ်
FlatList က data (array) + renderItem (item တစ်ခုစီကို ဘယ်လို ပြသမလဲ) + keyExtractor (item တစ်ခုစီရဲ့ unique key) ဆိုတဲ့ prop သုံးခုအဓိကနဲ့ အလုပ်လုပ်ပါတယ်။ ScrollView + .map() ကို item ရာနဲ့ချီရင် screen ပေါ် မမြင်ရသေးတဲ့ item တွေအားလုံးကို upfront render လုပ်ထားလို့ memory/performance ပြဿနာ ဖြစ်နိုင်ပါတယ် — FlatList ကတော့ virtualization (screen ပေါ်နီးစပ်တဲ့ item များကိုသာ render, အခြားကို recycle) သုံးပြီး item ရာချီ list တွေမှာ performance ကောင်းနေစေပါတယ်။
Problem → Why → Solution
ပြဿနာ — ဘာကြောင့် ဒီ concept လိုတာလဲ
keyExtractor ကို ချန်ထားပြီး array index ကို default key အဖြစ် အားကိုးခြင်း — list ပြောင်းလဲရင် bug ဖြစ်နိုင်
ဖြစ်လေ့ရှိတဲ့အမှား
Item ရာနဲ့ချီတဲ့ list ကို ScrollView + .map() နဲ့ ဆက်လုပ်နေခြင်း (performance ကျ)
Core Concept — အလုပ်လုပ်ပုံကိုနားလည်မယ်
Lists with FlatList ကို screen တစ်ခုအတွက် style syntax အဖြစ်သာမမြင်ဘဲ Yoga constraints, pixel density, font scaling, safe areas, keyboard, touch target, accessibility tree နဲ့ iOS/Android behavior ကွာခြားမှုတို့ပါသော native UI contract အဖြစ်နားလည်ပါ။
- JavaScript state
- Native component + layout
- Android / iOS UI
Lists with FlatList ရဲ့ React နဲ့ native platform behavior ကိုရှင်းပြနိုင်ရန်
အဓိက Syntax / Pattern
import { FlatList, View, Text, StyleSheet } from 'react-native';
const products = [
{ id: '1', name: 'T-Shirt', price: '15000 MMK' },
{ id: '2', name: 'Sneakers', price: '85000 MMK' },
{ id: '3', name: 'Backpack', price: '45000 MMK' },
];
export default function ProductList() {
return (
<FlatList
data={products}
keyExtractor={(item) => item.id}
renderItem={({ item }) => (
<View style={styles.card}>
<Text style={styles.name}>{item.name}</Text>
<Text>{item.price}</Text>
</View>
)}
/>
);
}
const styles = StyleSheet.create({
card: { padding: 16, borderBottomWidth: 1, borderColor: '#eee' },
name: { fontWeight: '600', fontSize: 16 },
});Lists with FlatList ကို screen တစ်ခုအတွက် style syntax အဖြစ်သာမမြင်ဘဲ Yoga constraints, pixel density, font scaling, safe areas, keyboard, touch target, accessibility tree နဲ့ iOS/Android behavior ကွာခြားမှုတို့ပါသော native UI contract အဖြစ်နားလည်ပါ။
Code ကို အဆင့်လိုက်ဖတ်မယ်
- JavaScript state
import { FlatList, View, Text, StyleSheet } from 'rea...FlatList က data (array) + renderItem (item တစ်ခုစီကို ဘယ်လို ပြသမလဲ) + keyExtractor (item တစ်ခုစီရဲ့ unique key) ဆိုတဲ့ prop သုံးခုအဓိကနဲ့ အလုပ်လုပ်ပါတယ်။ ScrollView + .map() ကို item ရာနဲ့ချီရင် screen ပေါ် မမြင်ရသေးတဲ့ item တွေအားလုံးကို upfront render လုပ်ထားလို့ memory/performance ပြဿနာ ဖြစ်နိုင်ပါတယ် — FlatList ကတော့ virtualization (screen ပေါ်နီးစပ်တဲ့ item များကိုသာ render, အခြားကို recycle) သုံးပြီး item ရာချီ list တွေမှာ performance ကောင်းနေစေပါတယ်။
- Native component + layout
<View style={styles.card}>Lists with FlatList ကို screen တစ်ခုအတွက် style syntax အဖြစ်သာမမြင်ဘဲ Yoga constraints, pixel density, font scaling, safe areas, keyboard, touch target, accessibility tree နဲ့ iOS/Android behavior ကွာခြားမှုတို့ပါသော native UI contract အဖြစ်နားလည်ပါ။
- Android / iOS UI
});Lists with FlatList example ကို Android/iOS, small/large screens, font scaling, dark mode, keyboard, screen reader, slow/offline network, denied permissions နဲ့ app background/foreground cases ဖြင့်စမ်းပါ။ DevTools, tests နဲ့ release build ကိုလိုအပ်သလိုအသုံးပြုပါ။
- JavaScript state
- Native component + layout
- Android / iOS UI
Success
Lists with FlatList ရဲ့ React နဲ့ native platform behavior ကိုရှင်းပြနိုင်ရန်
Failure
keyExtractor ကို ချန်ထားပြီး array index ကို default key အဖြစ် အားကိုးခြင်း — list ပြောင်းလဲရင် bug ဖြစ်နိုင်
ခဏလေး ဒီလိုပဲ စဉ်းစားကြည့်
FlatList က data (array) + renderItem (item တစ်ခုစီကို ဘယ်လို ပြသမလဲ) + keyExtractor (item တစ်ခုစီရဲ့ unique key) ဆိုတဲ့ prop သုံးခုအဓိကနဲ့ အလုပ်လုပ်ပါတယ်။ ScrollView + .map() ကို item ရာနဲ့ချီရင် screen ပေါ် မမြင်ရသေးတဲ့ item တွေအားလုံးကို upfront render လုပ်ထားလို့ memory/performance ပြဿနာ ဖြစ်နိုင်ပါတယ် — FlatList ကတော့ virtualization (screen ပေါ်နီးစပ်တဲ့ item များကိုသာ render, အခြားကို recycle) သုံးပြီး item ရာချီ list တွေမှာ performance ကောင်းနေစေပါတယ်။
လက်တွေ့ scenario နဲ့ ချိတ်ကြည့်မယ်
Chat app message list, e-commerce product list, social media feed — ဒီလိုမျိုး item အများကြီးပါတဲ့ list တိုင်းအတွက် FlatList က standard choice ပါ။ renderItem ထဲမှာ card component တစ်ခုစီကို return လုပ်ပြီး, keyExtractor ကို item._id ဒါမှမဟုတ် item.id ကို string ပြောင်းပြီး ပေးရပါတယ် — key မမှန်ရင် React reconciliation performance ကျနိုင်ပါတယ်။
Code နမူနာ
import { FlatList, View, Text, StyleSheet } from 'react-native';
const products = [
{ id: '1', name: 'T-Shirt', price: '15000 MMK' },
{ id: '2', name: 'Sneakers', price: '85000 MMK' },
{ id: '3', name: 'Backpack', price: '45000 MMK' },
];
export default function ProductList() {
return (
<FlatList
data={products}
keyExtractor={(item) => item.id}
renderItem={({ item }) => (
<View style={styles.card}>
<Text style={styles.name}>{item.name}</Text>
<Text>{item.price}</Text>
</View>
)}
/>
);
}
const styles = StyleSheet.create({
card: { padding: 16, borderBottomWidth: 1, borderColor: '#eee' },
name: { fontWeight: '600', fontSize: 16 },
});Product list ကို FlatList ဖြင့် scroll လုပ်ကြည့်နိုင်အောင် render နိုင်မည်။၅ မိနစ် စမ်းကြည့်
ကိုယ်ပိုင် data array (ဥပမာ favorite movies list) တစ်ခု ဖန်တီးပြီး FlatList နဲ့ render ကြည့်ပါ။
သတိလေးတစ်ချက်
FlatList ကို ScrollView ထဲမှာ nest မထားပါနှင့် (VirtualizedList nested inside ScrollView warning ပေါ်လာပါလိမ့်မယ်) — outer container ကို View သုံးပါ, ScrollView မလိုအပ်ပါဘူး။
Real-world usage
လက်တွေ့ Project မှာ ဘယ်လိုသုံးမလဲ
Lists with FlatList example ကို Android/iOS, small/large screens, font scaling, dark mode, keyboard, screen reader, slow/offline network, denied permissions နဲ့ app background/foreground cases ဖြင့်စမ်းပါ။ DevTools, tests နဲ့ release build ကိုလိုအပ်သလိုအသုံးပြုပါ။
Mini challenge
ကိုယ်တိုင်တည်ဆောက်ကြည့်ပါ
Lists with FlatList example ကို Android/iOS, small/large screens, font scaling, dark mode, keyboard, screen reader, slow/offline network, denied permissions နဲ့ app background/foreground cases ဖြင့်စမ်းပါ။ DevTools, tests နဲ့ release build ကိုလိုအပ်သလိုအသုံးပြုပါ။
- Example ကို copy မလုပ်ဘဲ ပြန်ရေးပါ
- Normal input နဲ့ edge case စမ်းပါ
- Run မလုပ်မီ compiler/output result ကိုခန့်မှန်းပါ
import { FlatList, View, Text, StyleSheet } from 'react-native';
const products = [
{ id: '1', name: 'T-Shirt', price: '15000 MMK' },
{ id: '2', name: 'Sneakers', price: '85000 MMK' },
{ id: '3', name: 'Backpack', price: '45000 MMK' },
];
export default function ProductList() {
return (
<FlatList
data={products}
keyExtractor={(item) => item.id}
renderItem={({ item }) => (
<View style={styles.card}>
<Text style={styles.name}>{item.name}</Text>
<Text>{item.price}</Text>
</View>
)}
/>
);
}
const styles = StyleSheet.create({
card: { padding: 16, borderBottomWidth: 1, borderColor: '#eee' },
name: { fontWeight: '600', fontSize: 16 },
});ရလဒ်: Lists with FlatList ရဲ့ React နဲ့ native platform behavior ကိုရှင်းပြနိုင်ရန်
အရိပ်အမြွက် ကြည့်ရန်
Lists with FlatList example ကို Android/iOS, small/large screens, font scaling, dark mode, keyboard, screen reader, slow/offline network, denied permissions နဲ့ app background/foreground cases ဖြင့်စမ်းပါ။ DevTools, tests နဲ့ release build ကိုလိုအပ်သလိုအသုံးပြုပါ။
Quick Quiz 1 / 3
Quick Quiz 2 / 3
Quick Quiz 3 / 3
ဒီသင်ခန်းစာမှာ မှတ်ထားရမယ့်အချက်များ
- JavaScript state → Native component + layout → Android / iOS UI
- Lists with FlatList ကို screen တစ်ခုအတွက် style syntax အဖြစ်သာမမြင်ဘဲ Yoga constraints, pixel density, font scaling, safe areas, keyboard, touch target, accessibility tree နဲ့ iOS/Android behavior ကွာခြားမှုတို့ပါသော native UI contract အဖြစ်နားလည်ပါ။
- keyExtractor ကို ချန်ထားပြီး array index ကို default key အဖြစ် အားကိုးခြင်း — list ပြောင်းလဲရင် bug ဖြစ်နိုင်
- Lists with FlatList example ကို Android/iOS, small/large screens, font scaling, dark mode, keyboard, screen reader, slow/offline network, denied permissions နဲ့ app background/foreground cases ဖြင့်စမ်းပါ။ DevTools, tests နဲ့ release build ကိုလိုအပ်သလိုအသုံးပြုပါ။
React Native Documentation — React Native