React Navigation - ParkEunwoo/seoul-smart-app GitHub Wiki

๋ฆฌ์•กํŠธ ๋„ค๋น„๊ฒŒ์ด์…˜

์„ค์น˜

  1. expo init ์œผ๋กœ ๋งŒ๋“  ํด๋”๋กœ ๋“ค์–ด๊ฐ„๋‹ค.

blank type์œผ๋กœ ๋งŒ๋“  ๊ฒฝ์šฐ : ํ•˜๋‚˜ํ•˜๋‚˜ ๋‹ค ์„ค์น˜ํ•ด์•ผํ•ฉ๋‹ˆ๋‹ค...

  • yarn add react-navigation
  • yarn add react-native-gesture-handler
  • yarn add react-navigation-tabs -> ์ƒ๋‹จ์— ํƒญ์„ ๋งŒ๋“ค๊ธฐ ์œ„ํ•ด์„œ
  • yarn add react-native-reanimated
  • yarn add react-navigation-stack

tab(?) type์ธ ๊ฒฝ์šฐ

  1. expo install react-native-gesture-handler react-native-reanimated ๋ช…๋ น์–ด๋ฅผ ํ†ตํ•ด react-navigation์„ ์„ค์น˜ํ•ฉ๋‹ˆ๋‹ค.

  2. react-navigation์€ tab navigation์„ ๊ธฐ๋ณธ์ ์œผ๋กœ ์Šคํฌ๋ฆฐ ํ•˜๋‹จ์— ๋งŒ๋“ค๋„๋ก ๋˜์–ด์žˆ์Šต๋‹ˆ๋‹ค. ์ €ํฌ๋Š” ์™€์ด์–ดํ”„๋ ˆ์ž„์ฒ˜๋Ÿผ ํƒญ์„ ์ƒ๋‹จ์— ๋งŒ๋“ค๊ธฐ ์œ„ํ•ด ์ถ”๊ฐ€๋กœ ๋” ์„ค์น˜ํ•  ๊ฒƒ์ด ์žˆ์Šต๋‹ˆ๋‹ค.

  3. ํ„ฐ๋ฏธ๋„ ์ฐฝ์—์„œ yarn add react-navigation-tabs ๋ช…๋ น์–ด๋กœ ์„ค์น˜ํ•ด์ฃผ์„ธ์š”.

  4. ๋‹ค์Œ์œผ๋กœ ํ„ฐ๋ฏธ๋„์ฐฝ์—์„œ yarn add react-navigation-reanimated ๋ช…๋ น์–ด๋กœ ์„ค์น˜ํ•ด์ฃผ์„ธ์š”.


ํด๋” ์‚ดํŽด๋ณด๊ธฐ

  1. App.js ํŒŒ์ผ
  2. components : screen์„ ๊ตฌ์„ฑํ•˜๋Š” ์ปดํฌ๋„ŒํŠธ๋“ค์ด ์žˆ๋Š” ํด๋”
  3. screens : ์•ฑ์—์„œ ๋ณด์—ฌ์ง€๋Š” ํ™”๋ฉด๋“ค์ด ์žˆ๋Š” ํด๋” (๋ฉ”์ธ, ์žฅ์†Œ, ํ™œ๋™..)
  4. navigation : ํ™”๋ฉด๋“ค์„ ๋„ค๋น„๊ฒŒ์ด์…˜์œผ๋กœ ๋“ฑ๋กํ•˜๋Š” ๋ถ€๋ถ„

(์˜ˆ์‹œ)

1. ํ™”๋ฉด ๋งŒ๋“ค๊ธฐ

  • ํ™”๋ฉด์€ ์ฒ˜์Œ ๋กœ๊ทธ์ธ ํŽ˜์ด์ง€, ๋กœ๊ทธ์ธ ํ›„ ๋ณด์ด๋Š” ๋ฉ”์ธํŽ˜์ด์ง€-์žฅ์†ŒํŽ˜์ด์ง€-ํ™œ๋™ํŽ˜์ด์ง€, ์žฅ์†ŒํŽ˜์ด์ง€์—์„œ detail๋ทฐ ํŽ˜์ด์ง€๋กœ ์ด๋ฃจ์–ด์ ธ์žˆ์Šต๋‹ˆ๋‹ค.
  • ํ™”๋ฉด์€ screens ํด๋”์— ๋งŒ๋“ค๋ฉด ๋ฉ๋‹ˆ๋‹ค.
  • LoginScreen.js, MainScreen.js , PlaceScreen.js, PlaceDetailScreen.js, APScreen.js ํŒŒ์ผ์„ ๋งŒ๋“ค์–ด ์ฃผ์„ธ์š”.

LoginScreen.js

import React  from 'react';
import { StyleSheet, Text, View, TouchableOpacity } from 'react-native'

export default function LoginScreen(props) {
    return (
        <View style={styles.container}>
            <Text>It's LoginScreen!</Text>
            <TouchableOpacity title="Login" onPress={() => _doLogin(props)}>
                <Text>Login</Text>
            </TouchableOpacity>
        </View>
    );
}


//ํŽ˜์ด์ง€ ์ด๋™ -> navigation.navigate(์ด๋™ํ•  ํŽ˜์ด์ง€)
function _doLogin(props){
    props.navigation.navigate('MainTabNavigator');
}

const styles = StyleSheet.create({
    container: {
        flex: 1,
        backgroundColor: '#fff',
        alignItems: 'center',
        justifyContent: 'center',
    },
}) 

MainScreen.js

import React from 'react';
import { StyleSheet, Text, View } from 'react-native';

export default function MainScreen() {
    return (
        <View style={styles.container}>
            <Text>It's Main!</Text>
        </View>
    );
}

const styles = StyleSheet.create({
    container: {
        flex: 1,
        backgroundColor: '#fff',
        alignItems: 'center',
        justifyContent: 'center',
    },
}) 

PlaceScreen.js

import React from 'react';
import { StyleSheet, Text, View, TouchableOpacity } from 'react-native';
import { createStackNavigator } from 'react-navigation-stack';



export default function PlaceScreen(props) {
    return (
        <View style={styles.container}>
            <Text>It's Place!</Text>
            <TouchableOpacity title="MovePlaceDetail" onPress={() => _goPlaceDetail(props)}>
                <Text>
                    Move to DetailView
                </Text>
            </TouchableOpacity>
        </View>
    );
}
//์žฅ์†ŒํŽ˜์ด์ง€์—์„œ ์žฅ์†Œ๋””ํ…Œ์ผํŽ˜์ด์ง€๋กœ ์ด๋™
function _goPlaceDetail(props){
    props.navigation.navigate('PlaceDetail');
}

const styles = StyleSheet.create({
    container: {
        flex: 1,
        backgroundColor: '#fff',
        alignItems: 'center',
        justifyContent: 'center',
    },
})

APScreen

...Main๊ณผ ๋™์ผ
export default function ActivityProgramScreen() {
    return (
        <View style={styles.container}>
            <Text>It's Activity and Program!</Text>
        </View>
    );
}
...Main๊ณผ ๋™์ผ

2. ํ™”๋ฉด์„ ์Šคํ…์— ๋“ฑ๋กํ•˜์—ฌ ๋ฉ”์ธ๊ณผ ํ•˜๋‹จ tab ๋งŒ๋“ค๊ธฐ

  • **react-navigation"์€ ๋„ค๋น„๊ฒŒ์ด์…˜์„ ๊ตฌํ˜„ํ•˜๋Š”๋ฐ ์Šคํƒ(stack) ๋„ค๋น„๊ฒŒ์ดํ„ฐ์œผ๋กœ ๊ตฌํ˜„ํ•ฉ๋‹ˆ๋‹ค.
  • ์‚ฌ์šฉํ•  screen๋“ค์„ stacknavigator์— ๋“ฑ๋กํ•˜๊ณ  ๋‹ค์–‘ํ•œ ํ•จ์ˆ˜๋“ค์„ ์ด์šฉํ•ด ํ•˜๋‹จ tab, ์ƒ๋‹จ tab, drawer ๋“ฑ์„ ๊ตฌ์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  • navigation ํด๋”์— tab navigation ์„ ๋งŒ๋“ค๊ธฐ ์œ„ํ•ด MainTabNavigator.js ํŒŒ์ผ์„ ๋งŒ๋“ค์–ด ์ฃผ์„ธ์š”.
  • ์ด๋ ‡๊ฒŒ ์‚ฌ์šฉํ•  ์Šคํฌ๋ฆฐ์„ stack์— ๋“ฑ๋กํ•˜๋Š” ์—ญํ• ์„ ํ•˜๋Š” ํ•จ์ˆ˜๊ฐ€ createStackNavigator ์ž…๋‹ˆ๋‹ค.
  • ์Šคํƒ๋„ค๋น„๊ฒŒ์ดํ„ฐ์— ๋“ฑ๋ก๋œ ์Šคํƒ(๊ทธ๋Ÿฌ๋‹ˆ๊นŒ ๋“ฑ๋ก๋œ ์Šคํฌ๋ฆฐ)๋“ค์„ ๋ชจ์•„ createBottomTabNavigator์„ ํ†ตํ•ด ํ•˜๋‹จ tab์„ ๋งŒ๋“ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

MainTabNavigator

import React from 'react';
import { Platform } from 'react-native';
import { createStackNavigator, createBottomTabNavigator } from 'react-navigation';

//์Šคํƒ๋„ค๋น„๊ฒŒ์ด์…˜์— ๋“ฑ๋กํ•  screen๋“ค์„ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ
import MainScreen from '../screens/MainScreen';
import PlaceScreen from '../screens/PlaceScreen';
import PlaceDetailScreen from '../screens/PlaceDetailScreen'
import APScreen from '../screens/APScreen';



const config = Platform.select({
  web: { headerMode: 'screen' },
  default: {},
});

//MainStack์ด๋ผ๋Š” ์ด๋ฆ„์œผ๋กœ StackNavigator๋ฅผ ๋งŒ๋“ญ๋‹ˆ๋‹ค.
//์ด ๋•Œ, MainScreenํ™”๋ฉด์„ Main์ด๋ผ๋Š” ์ด๋ฆ„์œผ๋กœ ๋“ฑ๋กํ•ฉ๋‹ˆ๋‹ค.
const MainStack = createStackNavigator(
  {
    Main: MainScreen,
  },
  config
);

MainStack.navigationOptions = {
  tabBarLabel: 'Main',
};

MainStack.path = '';

//PlaceStack์—๋Š” 2๊ฐ€์ง€์˜ ํ™”๋ฉด์„ ๋“ฑ๋กํ–ˆ์Šต๋‹ˆ๋‹ค.
//์™œ๋ƒํ•˜๋ฉด ์žฅ์†ŒํŽ˜์ด์ง€์—์„œ ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๋ฉด ์žฅ์†Œ์ƒ์„ธ๋ณด๊ธฐ๋กœ ๋„˜์–ด๊ฐ€์•ผํ•˜๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค.
const PlaceStack = createStackNavigator(
  {
    Place: PlaceScreen,
    PlaceDetail : PlaceDetailScreen,
  },
  config
);

PlaceStack.navigationOptions = {
  tabBarLabel: 'Place',
};

PlaceStack.path = '';

//์žฅ์†Œ์ƒ์„ธ๋ณด๊ธฐ๋กœ ๋„˜์–ด๊ฐ”์„ ๋•Œ ํ•˜๋‹จ ํƒญ์ด ์•ˆ๋ณด์ด๋„๋ก ํ•˜๋Š” ๊ธฐ๋Šฅ ๊ตฌํ˜„
PlaceStack.navigationOptions = ({ navigation }) => {
  let tabBarVisible = true;
  if (navigation.state.index > 0) {
    tabBarVisible = false;
  }

  return {
    tabBarVisible,
  };
};

const APStack = createStackNavigator(
  {
    AP: APScreen,
  },
  config
);

APStack.navigationOptions = {
  tabBarLabel: 'AP',
};

APStack.path = '';

//๊ฐ๊ฐ์˜ ํ™”๋ฉด๋“ค๋ฆฌ ๋“ฑ๋ก๋œ ์Šคํƒ์œผ๋กœ tanNavigator๋ผ๋Š” ํ•˜๋‹จtab์„ ๋งŒ๋“ญ๋‹ˆ๋‹ค.
const tabNavigator = createBottomTabNavigator({
  MainStack,
  PlaceStack,
  APStack,
});

tabNavigator.path = '';

//ํ•˜๋‹จ tab์„ ๋งŒ๋“ค์—ˆ์ง€๋งŒ ํ•˜๋‹จ tab๋งŒ์ด ์ „์ฒด ํ™”๋ฉด์ด ์•„๋‹™๋‹ˆ๋‹ค.
//๋”ฐ๋ผ์„œ, MainNav๋ผ๋Š” ์ด๋ฆ„์˜ ์ „์ฒดํ™”๋ฉด ๋™์ž‘ํ•˜๊ณ  tabNavigator๋ฅผ ๊ฐ–๋Š” ์Šคํƒ๋„ค๋น„๊ฒŒ์ดํ„ฐ๋ฅผ ๋งŒ๋“ค์–ด์•ผํ•ฉ๋‹ˆ๋‹ค.
//tabNavigator๋ฅผ exportํ•˜๋ฉด ํ•˜๋‹จtab๋งŒ ์ƒ๊ธฐ๋Š” ๊ผด์ด๊ฒ ์ฃ ..!
const MainNav = createStackNavigator({
  tabNavigator: {
    screen: tabNavigator,
    navigationOptions: ({navigation}) => ({
      header: null,
    })
  }
})


export default MainNav;

3. createAppContainer๋กœ ์‹ค์ œ ์ปดํฌ๋„ŒํŠธ๋กœ ๋ฐ˜ํ™˜

  • ์Šคํƒ๋„ค๋น„๊ฒŒ์ดํ„ฐ์— ๋“ฑ๋ก์„ ํ–ˆ์œผ๋ฉด ๊ทธ๊ฒƒ์„ ์ปดํฌ๋„ŒํŠธ๋กœ ๋ฐ˜ํ™˜ํ•ด์•ผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿด ๋•Œ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด createAppContainer ์ž…๋‹ˆ๋‹ค.
  • ์ด๋ฅผ ์œ„ํ•ด navigation ํด๋”์— AppNavigator.js ํŒŒ์ผ์„ ๋งŒ๋“ค์–ด ์ฃผ์„ธ์š”.
  • ์ €ํฌ๋Š” ๋กœ๊ทธ์ธ ํ™”๋ฉด์ด ๋จผ์ € ๋ณด์ด๊ณ  ๋กœ๊ทธ์ธํ™”๋ฉด์—์„œ ๋กœ๊ทธ์ธ ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๋ฉด ์ „์ฒดํ™”๋ฉด์ด ๋ณด์ด๋„๋ก ๊ตฌํ˜„ํ•˜๊ธฐ ์œ„ํ•ด createSwitchNavigatorํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.
  • createSwitchNavigator๋Š” ์Šคํƒ์˜ ๊ฐ€์žฅ ๋ฐ‘์— ์žˆ๋Š” ํ™”๋ฉด์„ ๋ง๊ทธ๋Œ€๋กœ switch! ํ•ด์ฃผ๋Š” ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค. ์Šคํƒ์˜ ๊ฐ€์žฅ ๋ฐ‘์— ์žˆ๋Š” ํ™”๋ฉด์ด๋ผ๋Š” ๊ฒƒ์€ ๋งจ ์ฒ˜์Œ ๋ณด์—ฌ์ง€๋Š” ํ™”๋ฉด์ด๊ฒ ์ฃ ..!
  • intialRouteName์„ ํ†ตํ•ด ์ฒ˜์Œ ๋ณด์—ฌ์ง€๋Š” ํ™”๋ฉด์„ ์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  • ์ฆ‰, ๋งจ ์ฒ˜์Œ ๋กœ๊ทธ์ธ ํ™”๋ฉด์ด ๋ณด์ผ ๋•Œ๋Š” ์Šคํƒ์˜ ๊ฐ€์žฅ ๋ฐ‘์ด loginscreen์ด์ง€๋งŒ

LoginScreen

<TouchableOpacity title="Login" onPress={() => _doLogin(props)}>
    <Text>Login</Text>
</TouchableOpacity>
...
function _doLogin(props){
    props.navigation.navigate('MainTabNavigator');
}
  • ๋กœ๊ทธ์ธ์ด ๋๋‚˜๊ณ  ์ „์ฒดํ™”๋ฉด์œผ๋กœ ์ด๋™ํ•˜๋ฉด ์Šคํƒ์˜ ๊ฐ€์žฅ ๋ฐ‘์— ์žˆ๋Š” ํ™”๋ฉด์ด MainTabNavigator๊ฐ€ ๋ฉ๋‹ˆ๋‹ค.

AppNavigator

import React from 'react';
import LoginScreen from '../screens/LoginScreen';
import { createAppContainer, createSwitchNavigator } from 'react-navigation';

import MainTabNavigator from './MainTabNavigator';

export default createAppContainer(
  createSwitchNavigator({
    LoginScreen,
    MainTabNavigator,
  },
  {
    initialRouteName: 'LoginScreen',
  }
  )
);

4. ์‹ค์ œ ํ™”๋ฉด์— ์ ์šฉ

  • ์‹ค์ œ ํ™”๋ฉด์— ๋‚˜์˜ค๋Š” ๊ฒƒ๋“ค์€ App.js์— ๋„ฃ์–ด์ค˜์•ผ ํ•ฉ๋‹ˆ๋‹ค.

App.js

import React from 'react';
import { StyleSheet, View } from 'react-native';
import AppNavigator from './navigation/AppNavigator';


export default function App() {
  return (
      <AppNavigator/>
  );
}


const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: '#fff',
    alignItems: 'center',
    justifyContent: 'center',
  },
});

์ •๋ฆฌ

  1. react-navigation์€ ํ™”๋ฉด(screen)๋“ค์„ ์Šคํƒ๋„ค๋น„๊ฒŒ์ดํ„ฐ(stacknavigator)๋กœ ๊ด€๋ฆฌํ•ฉ๋‹ˆ๋‹ค. ์ฆ‰, ์ƒˆ๋กœ์šด ํ™”๋ฉด์— ๋“ค์–ด๊ฐˆ ์ˆ˜๋ก ์Šคํƒ์— ์Œ“์ธ๋‹ค..๋ผ๊ณ  ์ƒ๊ฐํ•˜๋ฉด ๋  ๊ฒƒ ๊ฐ™์•„์š”!
  2. ๋”ฐ๋ผ์„œ ์‚ฌ์šฉํ•  ํ™”๋ฉด๋“ค์„ createStackNavigator๋กœ ์Šคํƒ๋„ค๋น„๊ฒŒ์ดํ„ฐ์— ๋“ฑ๋กํ•ด์•ผํ•ฉ๋‹ˆ๋‹ค. ํŽ˜์ด์ง€ ์ด๋™(navigation.navigate)์‹œ ํ™”๋ฉด์ด ์Šคํƒ์— ๋“ฑ๋ก๋œ ํ™”๋ฉด์ด์–ด์•ผ ์ด๋™์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค!!
  3. ๋“ฑ๋ก๋œ ํ™”๋ฉด๋“ค์„ createBottomTabNavigator์™€ ๊ฐ™์€ ํ•จ์ˆ˜๋“ค๋กœ ํ•˜๋‹จํƒญ, ํ—ค๋”, ๋“œ๋กœ์›Œ ๋“ฑ๋“ฑ์„ ๋งŒ๋“ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  4. ๋งŒ๋“  ํ›„์—๋Š” ์ปดํฌ๋„ŒํŠธ๋กœ ๋ฐ˜ํ™˜ํ•ด์•ผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. createAppContainer๋ฅผ ์‚ฌ์šฉํ•ด์•ผํ•ฉ๋‹ˆ๋‹ค.
  5. ํŽ˜์ด์ง€ ๊ฐ„ ์ด๋™

์Šคํƒ๋„ค๋น„๊ฒŒ์ดํ„ฐ์— ๋“ฑ๋ก๋œ ๋ชจ๋“  screen๋“ค์€ navigation prop์„ ๊ฐ–์Šต๋‹ˆ๋‹ค. navigation์— ์žˆ๋Š” ๋ฉ”์†Œ๋“œ navigate๋ฅผ ํ†ตํ•ด ์ด๋™ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ ์™ธ์—๋„ push, goBack, popToTop ๋“ฑ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฉ”์†Œ๋“œ์˜ ์ฒซ๋ฒˆ์งธ ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ์ด๋™ํ•  ํŽ˜์ด์ง€๋ฅผ, ๋‘๋ฒˆ์งธ ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ๋Š” ์–ด๋–ค ๊ฐ’์„ ๊ฐ์ฒด ํ˜•ํƒœ๋กœ ์ „๋‹ฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

  1. ์ƒ์„ธํŽ˜์ด์ง€ ๋“ค์–ด๊ฐˆ ๋•Œ ํ•˜๋‹จtab ์—†์• ๊ธฐ

๋‹ค์–‘ํ•œ ๊ฒฝ์šฐ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค..์•„๋ž˜ ๋งํฌ๋ฅผ ์ฐธ๊ณ ํ•ด์ฃผ์„ธ์š”! Navigation options resolution


์—ฌ๊ธฐ๋ถ€ํ„ฐ๋Š” ๊ณต์‹๋ฌธ์„œ๋ฅผ ๋ณด๊ณ  ์ •๋ฆฌํ•œ๊ฑฐ๋ผ ์ฐธ๊ณ ํ•ด์ฃผ์„ธ์š”!

์Šคํƒ ๋„ค๋น„๊ฒŒ์ดํ„ฐ ๋งŒ๋“ค๊ธฐ

1. createStackNavigator ํ•จ์ˆ˜

  • ๋ฆฌ์•กํŠธ ์ปดํฌ๋„ŒํŠธ๋ฅผ ๋ฐ˜ํ™˜

2. createAppContainer ํ•จ์ˆ˜

  • createStackNavigator๋กœ ๋งŒ๋“ค์–ด์ง„ ๋ฆฌ์•กํŠธ ์ปดํฌ๋„ŒํŠธ๋ฅผ ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ๋ฐ›์•„์„œ ์ด ์ปดํฌ๋„ŒํŠธ๋ฅผ ๋ฐ˜ํ™˜

<์˜ˆ์‹œ>

import React from 'react';
import { View, Text } from 'react-native';
import { createAppContainer } from 'react-navigation';
import { createStackNavigator } from 'react-navigation-stack';

class HomeScreen extends React.Component {
  render() {
    return (
      <View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
        <Text>Home Screen</Text>
      </View>
    );
  }
}

class DetailsScreen extends React.Component {
  render() {
    return (
      <View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
        <Text>Details Screen</Text>
      </View>
    );
  }
}

const AppNavigator = createStackNavigator(
  {
    Home: HomeScreen, /*Homescreen ์ปดํฌ๋„ŒํŠธ๋ฅผ ๋„ค๋น„๊ฒŒ์ดํ„ฐ์— ๋“ฑ๋ก*/
    Details: DetailsScreen, /*DetailsScreen ์ปดํฌ๋„ŒํŠธ๋ฅผ ๋„ค๋น„๊ฒŒ์ดํ„ฐ์— ๋“ฑ๋ก*/
  },
  {
    initialRouteName: 'Home',
  }
);
/*
Home๊ณผ Details์ด๋ผ๋Š” 2๊ฐœ์˜ route๊ฐ€ ์ƒ์„ฑ๋˜์—ˆ๋‹ค.
Home route(key)๋Š” HomeScreen ์ปดํฌ๋„ŒํŠธ(value)์— ํ•ด๋‹นํ•˜๊ณ , Details route(key) ๋Š” DetailsScreen(value)์— ํ•ด๋‹น*/

const AppContainer = createAppContainer(AppNavigator); 
/*๋„ค๋น„๊ฒŒ์ดํ„ฐ๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” createAppContainer๋กœ ๋ฐ˜ํ™˜ํ•ด์•ผ ํ•œ๋‹ค*/

export default class App extends React.Component {
  render() {
    return <AppContainer />; /*AppContainer ์ปดํฌ๋„ŒํŠธ๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค*/
  }
}

ํŽ˜์ด์ง€(Screen) ๊ฐ„ ์ด๋™

  • navigate ํ•จ์ˆ˜ : ์ด screen์œผ๋กœ ์ด๋™ํ•˜๋ผ

<์˜ˆ์‹œ>

import React from 'react';
import { Button, View, Text } from 'react-native';
import { createStackNavigator, createAppContainer } from 'react-navigation';

   class HomeScreen extends React.Component {
      render() {
        return (
          <View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
            <Text>Home Screen</Text>
            <Button
              title="Go to Details"
              onPress={() => this.props.navigation.navigate('Details')} 
            />
          </View>
        );
      }
    }
  1. this.props.navigation : stack ๋‚ด๋น„๊ฒŒ์ดํ„ฐ์˜ ๋ชจ๋“  screen ์ปดํฌ๋„ŒํŠธ๋กœ ์ „๋‹ฌ๋จ
  2. navigate('Details') : 'Detail' route๋กœ ์ด๋™
  • push ํ•จ์ˆ˜

<์˜ˆ์‹œ>

 <Button
     title="Go to Details... again"
      onPress={() => this.props.navigation.push('Details')}
 />
  1. navigate ํ•จ์ˆ˜๋Š” ๋ถ€๋ฅธ route๊ฐ€ ์ƒˆ๋กœ์šด ๊ฒƒ(stack์— ์—†์„ ๋•Œ)์ผ ๋•Œ๋งŒ stack์— route๋ฅผ ์ถ”๊ฐ€ํ•จ
  2. push ํ•จ์ˆ˜๋Š” ์ƒ๊ด€์—†์ด ๊ณ„์† route๋ฅผ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ์Œ

์˜ˆ๋ฅผ ๋“ค์–ด์„œ, ํ˜„์žฌ ํ™”๋ฉด์—์„œ ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ๊ฐ’์„ ๋ฐ›์•„์„œ ์„ธ๋ถ€์ ์ธ ๋‹ค๋ฅธ ํ™”๋ฉด์„ ์ถ”๊ฐ€ํ•˜๊ณ  ์‹ถ์„ ๊ฒฝ์šฐ, (์œ„์˜ ์˜ˆ์‹œ์—๋Š” ์ถ”๊ฐ€ํ•  ๊ฐ’์ด ์—†์Šต๋‹ˆ๋‹ค) navigate๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด stack์— ์ด๋ฏธ ์žˆ๋Š” ํ™”๋ฉด์ด๋ฏ€๋กœ ์ถ”๊ฐ€๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿด ๋•Œ๋Š” push!

  • goBack ํ•จ์ˆ˜
  1. ๋’ค๋กœ๊ฐ€๊ธฐ ๋ฒ„ํŠผ์€ ํ—ค๋” ๋ฐ”์— ์ž๋™์ ์œผ๋กœ ๋ณด์—ฌ์ง€์ง€๋งŒ ์ง์ ‘ ํ•ธ๋“ค๋งํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด this.props.navigation.push('Route ์ด๋ฆ„') ๋ผ๊ณ  ์„ค์ •ํ•˜๋ฉด ๋จ

  2. ์ฒ˜์Œ ํ™”๋ฉด(๋ฉ”์ธ ํ™”๋ฉด)์œผ๋กœ ๋Œ์•„๊ฐ€๋ ค๋ฉด navigate ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๊ฑฐ๋‚˜ this.props.navigation.popToTop() ์„ ์‚ฌ์šฉ -> stack์—์„œ ์ฒ˜์Œ ํ™”๋ฉด์œผ๋กœ ๋Œ์•„๊ฐ€๊ฒŒ ํ•ด์คŒ


routes๋กœ ๋งค๊ฐœ๋ณ€์ˆ˜ ์ „๋‹ฌ

  1. ์ „๋‹ฌ: ํ•จ์ˆ˜๋ฅผ navigate ํ•จ์ˆ˜์˜ 2๋ฒˆ์งธ ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ์ „๋‹ฌํ•  ๊ฒƒ์„ ๊ฐ์ฒด ํ˜•ํƒœ๋กœ ๋„ฃ์–ด์คŒ this.props.navigation.navigate('route ์ด๋ฆ„', { ์ „๋‹ฌ๋‚ด์šฉ })
  2. ์ฝ๊ธฐ: this.props.navigation.getParam('ํŒŒ๋ผ๋ฏธํ„ฐ ์ด๋ฆ„','default value')

<์˜ˆ์‹œ>

class HomeScreen extends React.Component {
  render() {
    return (
      <View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
        <Text>Home Screen</Text>
        <Button
          title="Go to Details"
          onPress={() => {
            /* Details route๋กœ ๋„˜๊ฒจ์ค„ ๋•Œ ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ๊ฐ’์„ ๋„ฃ์–ด์ค€๋‹ค */
            this.props.navigation.navigate('Details', {
              itemId: 86,
              otherParam: 'anything you want here',
            });
          }}
        />
      </View>
    );
  }
}

class DetailsScreen extends React.Component {
  render() {
    /* ํŒŒ๋ผ๋ฏธํ„ฐ ๋‚ด์šฉ์„ ๊ฐ€์ ธ์˜จ๋‹ค */
    const { navigation } = this.props;
    const itemId = navigation.getParam('itemId', 'NO-ID');
    const otherParam = navigation.getParam('otherParam', 'some default value');

    return (
      <View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
        <Text>Details Screen</Text>
        <Text>itemId: {JSON.stringify(itemId)}</Text>
        <Text>otherParam: {JSON.stringify(otherParam)}</Text>
        <Button
          title="Go to Details... again"
          onPress={() =>
            this.props.navigation.push('Details', {
              itemId: Math.floor(Math.random() * 100),
            })
          }
        />
        <Button
          title="Go to Home"
          onPress={() => this.props.navigation.navigate('Home')}
        />
        <Button
          title="Go back"
          onPress={() => this.props.navigation.goBack()}
        />
      </View>
    );
  }
}

์•„์ง ๋งŽ์ด ๋ชป์จ์„œ.. ๊ณ„์† ์ถ”๊ฐ€ํ•˜๊ฒ ์Šต๋‹ˆ๋‹คใ…œใ…œ

โš ๏ธ **GitHub.com Fallback** โš ๏ธ