ရိုးရိုးလေး ရှင်းပြမယ်
Fetching Data with API ကို ရိုးရိုးလေးနားလည်မယ်
React Native မှာ fetch API က web browser နဲ့ syntax တူတူ ရရှိနိုင်ပါတယ် (built-in) — API call ကို component mount ဖြစ်တဲ့အချိန် တစ်ခါပဲ run ချင်ရင် useEffect(() => { ... }, []) (empty dependency array) ထဲမှာ ထည့်ရေးရပါတယ်။ Loading state (data ရမီအလား ပြသဖို့) နဲ့ error state (fail ရင် ပြသဖို့) ကို useState ၂ ခုနဲ့ ပေါင်းသုံးရင် complete data-fetching pattern ရပါတယ်။
Problem → Why → Solution
ပြဿနာ — ဘာကြောင့် ဒီ concept လိုတာလဲ
useEffect ရဲ့ dependency array ([]) ကို ချန်ထားလိုက်ရင် API call ကို render တိုင်း ထပ်ခါထပ်ခါ run ဖြစ်ခြင်း
ဖြစ်လေ့ရှိတဲ့အမှား
try/catch မပါဘဲ fetch call ကို run ပြီး network error ဖြစ်ချိန် app crash ဖြစ်ခြင်း
Core Concept — အလုပ်လုပ်ပုံကိုနားလည်မယ်
Fetching Data with API ကို permission, loading, empty, offline, timeout, cancellation, retry, app background/foreground နဲ့ stale-data behavior ပါသော lifecycle-aware flow အဖြစ်တည်ဆောက်ပါ။ Subscription, timer နဲ့ request cleanup ကိုမမေ့ပါနှင့်။
- Request / permission
- Native module boundary
- Result / error / cleanup
Fetching Data with API ရဲ့ React နဲ့ native platform behavior ကိုရှင်းပြနိုင်ရန်
အဓိက Syntax / Pattern
import { useState, useEffect } from 'react';
import { View, Text, ActivityIndicator, FlatList } from 'react-native';
export default function PostsList() {
const [posts, setPosts] = useState([]);
const [loading, setLoading] = useState(true);
const [error, setError] = useState(null);
useEffect(() => {
fetch('https://jsonplaceholder.typicode.com/posts')
.then((res) => res.json())
.then((data) => setPosts(data))
.catch((err) => setError(err.message))
.finally(() => setLoading(false));
}, []);
if (loading) return <ActivityIndicator size="large" />;
if (error) return <Text>Error: {error}</Text>;
return (
<FlatList
data={posts}
keyExtractor={(item) => item.id.toString()}
renderItem={({ item }) => <Text>{item.title}</Text>}
/>
);
}Fetching Data with API ကို permission, loading, empty, offline, timeout, cancellation, retry, app background/foreground နဲ့ stale-data behavior ပါသော lifecycle-aware flow အဖြစ်တည်ဆောက်ပါ။ Subscription, timer နဲ့ request cleanup ကိုမမေ့ပါနှင့်။
Code ကို အဆင့်လိုက်ဖတ်မယ်
- Request / permission
import { useState, useEffect } from 'react';React Native မှာ fetch API က web browser နဲ့ syntax တူတူ ရရှိနိုင်ပါတယ် (built-in) — API call ကို component mount ဖြစ်တဲ့အချိန် တစ်ခါပဲ run ချင်ရင် useEffect(() => { ... }, []) (empty dependency array) ထဲမှာ ထည့်ရေးရပါတယ်။ Loading state (data ရမီအလား ပြသဖို့) နဲ့ error state (fail ရင် ပြသဖို့) ကို useState ၂ ခုနဲ့ ပေါင်းသုံးရင် complete data-fetching pattern ရပါတယ်။
- Native module boundary
.finally(() => setLoading(false));Fetching Data with API ကို permission, loading, empty, offline, timeout, cancellation, retry, app background/foreground နဲ့ stale-data behavior ပါသော lifecycle-aware flow အဖြစ်တည်ဆောက်ပါ။ Subscription, timer နဲ့ request cleanup ကိုမမေ့ပါနှင့်။
- Result / error / cleanup
}Fetching Data with API 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 ကိုလိုအပ်သလိုအသုံးပြုပါ။
- Request / permission
- Native module boundary
- Result / error / cleanup
Success
Fetching Data with API ရဲ့ React နဲ့ native platform behavior ကိုရှင်းပြနိုင်ရန်
Failure
useEffect ရဲ့ dependency array ([]) ကို ချန်ထားလိုက်ရင် API call ကို render တိုင်း ထပ်ခါထပ်ခါ run ဖြစ်ခြင်း
ခဏလေး ဒီလိုပဲ စဉ်းစားကြည့်
React Native မှာ fetch API က web browser နဲ့ syntax တူတူ ရရှိနိုင်ပါတယ် (built-in) — API call ကို component mount ဖြစ်တဲ့အချိန် တစ်ခါပဲ run ချင်ရင် useEffect(() => { ... }, []) (empty dependency array) ထဲမှာ ထည့်ရေးရပါတယ်။ Loading state (data ရမီအလား ပြသဖို့) နဲ့ error state (fail ရင် ပြသဖို့) ကို useState ၂ ခုနဲ့ ပေါင်းသုံးရင် complete data-fetching pattern ရပါတယ်။
လက်တွေ့ scenario နဲ့ ချိတ်ကြည့်မယ်
API call ကို try/catch ဖြင့် wrap ရမှာက error handling အတွက် အရေးကြီးပါတယ် — network connection မရှိတဲ့အချိန် (mobile user တွေ signal ပျက်တတ်တယ်) fetch က throw လုပ်နိုင်ပါတယ်, catch မလုပ်ရင် app crash ဖြစ်နိုင်ပါတယ်။ ActivityIndicator component (built-in loading spinner) ကို loading state true ချိန်မှာ ပြသပြီး, data ရောက်မှ actual content ကို render ပါတယ်။
Code နမူနာ
import { useState, useEffect } from 'react';
import { View, Text, ActivityIndicator, FlatList } from 'react-native';
export default function PostsList() {
const [posts, setPosts] = useState([]);
const [loading, setLoading] = useState(true);
const [error, setError] = useState(null);
useEffect(() => {
fetch('https://jsonplaceholder.typicode.com/posts')
.then((res) => res.json())
.then((data) => setPosts(data))
.catch((err) => setError(err.message))
.finally(() => setLoading(false));
}, []);
if (loading) return <ActivityIndicator size="large" />;
if (error) return <Text>Error: {error}</Text>;
return (
<FlatList
data={posts}
keyExtractor={(item) => item.id.toString()}
renderItem={({ item }) => <Text>{item.title}</Text>}
/>
);
}App ဖွင့်ချိန် loading spinner ပေါ်ပြီး, data ရောက်မှ post title list ကို FlatList ဖြင့် ပြသမည်။၅ မိနစ် စမ်းကြည့်
Public API (ဥပမာ jsonplaceholder.typicode.com) ကနေ data fetch လုပ်ပြီး loading/error state ၂ ခုစလုံး ပါတဲ့ screen တစ်ခု ကိုယ်တိုင် ဆောက်ကြည့်ပါ။
သတိလေးတစ်ချက်
API key ဒါမှမဟုတ် sensitive token ကို client code ထဲ hardcode မလုပ်ပါနှင့် (Cybersecurity tutorial ရဲ့ API Key Management lesson ကို ပြန်သတိရပါ) — mobile app ကို decompile လုပ်ရင် hardcoded secret ကို တွေ့နိုင်ပါတယ်။
Real-world usage
လက်တွေ့ Project မှာ ဘယ်လိုသုံးမလဲ
Fetching Data with API 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
ကိုယ်တိုင်တည်ဆောက်ကြည့်ပါ
Fetching Data with API 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 { useState, useEffect } from 'react';
import { View, Text, ActivityIndicator, FlatList } from 'react-native';
export default function PostsList() {
const [posts, setPosts] = useState([]);
const [loading, setLoading] = useState(true);
const [error, setError] = useState(null);
useEffect(() => {
fetch('https://jsonplaceholder.typicode.com/posts')
.then((res) => res.json())
.then((data) => setPosts(data))
.catch((err) => setError(err.message))
.finally(() => setLoading(false));
}, []);
if (loading) return <ActivityIndicator size="large" />;
if (error) return <Text>Error: {error}</Text>;
return (
<FlatList
data={posts}
keyExtractor={(item) => item.id.toString()}
renderItem={({ item }) => <Text>{item.title}</Text>}
/>
);
}ရလဒ်: Fetching Data with API ရဲ့ React နဲ့ native platform behavior ကိုရှင်းပြနိုင်ရန်
အရိပ်အမြွက် ကြည့်ရန်
Fetching Data with API 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
ဒီသင်ခန်းစာမှာ မှတ်ထားရမယ့်အချက်များ
- Request / permission → Native module boundary → Result / error / cleanup
- Fetching Data with API ကို permission, loading, empty, offline, timeout, cancellation, retry, app background/foreground နဲ့ stale-data behavior ပါသော lifecycle-aware flow အဖြစ်တည်ဆောက်ပါ။ Subscription, timer နဲ့ request cleanup ကိုမမေ့ပါနှင့်။
- useEffect ရဲ့ dependency array ([]) ကို ချန်ထားလိုက်ရင် API call ကို render တိုင်း ထပ်ခါထပ်ခါ run ဖြစ်ခြင်း
- Fetching Data with API 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