Thuta Learning
ရှာဖွေရန်
IntermediateMobile Developmentintermediate

Fetching Data with API

စိတ်လျှော့ပါ။ ဒီခန်းကို စာအုပ်လိုမဟုတ်ဘဲ စကားပြောသလိုပဲ၊ နားလည်လွယ်အောင် ရှင်းပါမယ်။

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

  • Fetching Data with API ကို ကြောက်စရာမလိုအောင် နားလည်မယ်
  • ကိုယ်တိုင် code ရေးပြီး Expo Go ပေါ်မှာ run ကြည့်တတ်မယ်
  • Real app project ထဲမှာ ဒီ concept ကို ချက်ချင်း အသုံးချတတ်မယ်

ခဏလေး ဒီလိုပဲ စဉ်းစားကြည့်

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 ကို တွေ့နိုင်ပါတယ်။

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

  • useEffect ရဲ့ dependency array ([]) ကို ချန်ထားလိုက်ရင် API call ကို render တိုင်း ထပ်ခါထပ်ခါ run ဖြစ်ခြင်း
  • try/catch မပါဘဲ fetch call ကို run ပြီး network error ဖြစ်ချိန် app crash ဖြစ်ခြင်း

အခု ကိုယ်တိုင် စမ်းကြည့်

Public API (ဥပမာ jsonplaceholder.typicode.com) ကနေ data fetch လုပ်ပြီး loading/error state ၂ ခုစလုံး ပါတဲ့ screen တစ်ခု ကိုယ်တိုင် ဆောက်ကြည့်ပါ။

You'll know it worked when: App ဖွင့်ချိန် loading spinner ပေါ်ပြီး, data ရောက်မှ post title list ကို FlatList ဖြင့် ပြသမည်။

Fetching Data with API | Thuta Learning