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

Navigation Basics (Stack Navigator)

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

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

  • Navigation Basics (Stack Navigator) ကို ကြောက်စရာမလိုအောင် နားလည်မယ်
  • ကိုယ်တိုင် code ရေးပြီး Expo Go ပေါ်မှာ run ကြည့်တတ်မယ်
  • Real app project ထဲမှာ ဒီ concept ကို ချက်ချင်း အသုံးချတတ်မယ်

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

React Navigation က React Native app အတွက် de facto standard navigation library ပါ — Stack Navigator က screen တွေကို 'stack' (တစ်ခုပေါ်တစ်ခု ပုံ) အနေနဲ့ manage လုပ်ပါတယ်, screen A ကနေ B ကို navigate လုပ်ရင် B ကို stack ပေါ်ကနေ push, back button ကနေ pop ပြန်လုပ်ပါတယ် (mobile app ရဲ့ familiar navigation pattern)။ navigation.navigate('ScreenName') နဲ့ screen ကူးပြီး, navigation.goBack() နဲ့ ပြန်သွားနိုင်ပါတယ်။

လက်တွေ့ scenario နဲ့ ချိတ်ကြည့်မယ်

npm install @react-navigation/native @react-navigation/native-stack ကို install လုပ်ပြီး NavigationContainer ထဲမှာ Stack.Navigator ကို wrap ရပါတယ် — screen တစ်ခုစီကို Stack.Screen component အဖြစ် register ရပါတယ် (name prop + component prop)။ Screen A ရဲ့ button press ကနေ navigation.navigate('ScreenB') ခေါ်ရင် Screen B ဆီ transition animation နဲ့ ရွှေ့သွားပါလိမ့်မယ်။

Code နမူနာ

javascript
import { NavigationContainer } from '@react-navigation/native';
import { createNativeStackNavigator } from '@react-navigation/native-stack';
import HomeScreen from './HomeScreen';
import DetailScreen from './DetailScreen';

const Stack = createNativeStackNavigator();

export default function App() {
  return (
    <NavigationContainer>
      <Stack.Navigator>
        <Stack.Screen name="Home" component={HomeScreen} />
        <Stack.Screen name="Detail" component={DetailScreen} />
      </Stack.Navigator>
    </NavigationContainer>
  );
}

// Inside HomeScreen:
// <Button title="View Detail" onPress={() => navigation.navigate('Detail')} />
You should see
Home screen ရဲ့ button ကို press လိုက်ရင် Detail screen ဆီ transition animation ဖြင့် navigate ဖြစ်သွားမည်။

၅ မိနစ် စမ်းကြည့်

Screen ၂ ခု (Home, Detail) ကို ဖန်တီးပြီး React Navigation Stack Navigator ဖြင့် ချိတ်ဆက်ကြည့်ပါ။

သတိလေးတစ်ချက်

navigation.navigate('WrongScreenName') ရေးရင် compile error မတက်ဘဲ runtime crash ဖြစ်နိုင်ပါတယ် — Stack.Screen name prop နဲ့ navigate() ထဲက string ကို တိကျစွာ တူညီအောင် ရေးပါ။

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

  • NavigationContainer ကို App ရဲ့ root မှာ wrap ထားရန် မေ့ထားခြင်း — navigation.navigate မအလုပ်ဖြစ်
  • navigation prop ကို screen component ထဲ ဘယ်လိုရောက်လာလဲ နားမလည်ဘဲ manually pass ကြိုးစားခြင်း (React Navigation က automatic ပေးထားပါတယ်)

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

Screen ၂ ခု (Home, Detail) ကို ဖန်တီးပြီး React Navigation Stack Navigator ဖြင့် ချိတ်ဆက်ကြည့်ပါ။

You'll know it worked when: Home screen ရဲ့ button ကို press လိုက်ရင် Detail screen ဆီ transition animation ဖြင့် navigate ဖြစ်သွားမည်။

Navigation Basics (Stack Navigator) | Thuta Learning