Thuta Learning
React Native
IntermediateMobile Developmentအလယ်အလတ်

Fetching Data with API

React Nativeသင်ခန်းစာ 1320 မိနစ်

ဒီခန်းပြီးရင် ဘာတတ်သွားမလဲ

  • Fetching Data with API ရဲ့ React နဲ့ native platform behavior ကိုရှင်းပြနိုင်ရန်
  • Accessibility, failure နဲ့ lifecycle cases စမ်းနိုင်ရန်
  • Maintainable, testable နဲ့ performant React Native code ရေးနိုင်ရန်

ရိုးရိုးလေး ရှင်းပြမယ်

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 ကိုမမေ့ပါနှင့်။

Mental Model
  1. Request / permission
  2. Native module boundary
  3. Result / error / cleanup

Fetching Data with API ရဲ့ React နဲ့ native platform behavior ကိုရှင်းပြနိုင်ရန်

အဓိက Syntax / Pattern

javascript
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 ကို အဆင့်လိုက်ဖတ်မယ်

  1. Request / permissionimport { 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 ရပါတယ်။

  2. 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 ကိုမမေ့ပါနှင့်။

  3. 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 ကိုလိုအပ်သလိုအသုံးပြုပါ။

Execution Flow
  1. Request / permission
  2. Native module boundary
  3. 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 နမူနာ

javascript
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>}
    />
  );
}
You should see
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 ကိုခန့်မှန်းပါ
javascript
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

Fetching Data with API ရဲ့ အဓိကသဘောတရားက ဘာလဲ။

Quick Quiz 2 / 3

ဘယ်အမှားကို အရင်ဆုံးရှောင်သင့်လဲ။

Quick Quiz 3 / 3

Real project မှာ ဘယ်လိုအသုံးချသင့်လဲ။

ဒီသင်ခန်းစာမှာ မှတ်ထားရမယ့်အချက်များ

  • 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

ဒီနေရာမှာ လူအများမှားတတ်တယ်

  • useEffect ရဲ့ dependency array ([]) ကို ချန်ထားလိုက်ရင် API call ကို render တိုင်း ထပ်ခါထပ်ခါ run ဖြစ်ခြင်း
  • try/catch မပါဘဲ fetch call ကို run ပြီး network error ဖြစ်ချိန် app crash ဖြစ်ခြင်း
  • Simulator တစ်ခု၏ happy path မှန်ရုံဖြင့် real devices, release builds, permissions, accessibility, offline နဲ့ lifecycle paths အားလုံးမှန်ပြီဟုယူဆခြင်း။

လက်တွေ့လေ့ကျင့်ခန်း

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 ကိုလိုအပ်သလိုအသုံးပြုပါ။

အလုပ်ဖြစ်ကြောင်း ဒီလိုသိမယ်: App ဖွင့်ချိန် loading spinner ပေါ်ပြီး, data ရောက်မှ post title list ကို FlatList ဖြင့် ပြသမည်။

နောက်ဆုံးပြင်ဆင်ရက်

Progress ကို ဤ browser (ဤစက်) တွင်သာ သိမ်းထားပါသည်။ အခြားစက်များနှင့် sync မလုပ်ပါ။

ဤသင်ခန်းစာတွင် အမှားတွေ့ပါက အကြောင်းကြားရန်သင့် email app ကို ဤသင်ခန်းစာလိပ်စာ ဖြည့်ပြီးသားဖြင့် ဖွင့်ပေးပါမည် — သင်ကိုယ်တိုင် ပို့မှသာ ပို့မည်ဖြစ်သည်။

Fetching Data with API | Thuta Learning