
다음 화면으로 이동한다면 화면 상으로는 이전 화면이 사라지고 새로운 화면이 보이지만, 구조적으로는 컨테이너에 최초 스크린이 push 되고 다음 화면이 push 되어서 컨테이너에 2개의 노드가 쌓인 격이다.
(출처: https://velog.io/@naseriansuzie/imcourseTIL28)
npm install @react-navigation/stack
createStackNavigator
Screen과 Navigator 속성을 포함하는 객체를 반환하는 함수Screen과 Navigator 는 navigator를 구성하는데 사용되는 리액트 컴포넌트Navigator는 Screen을 child로 반드시 포함하고 있어야 함NavigationContainer
import * as React from 'react';
import { View, Text } from 'react-native';
import { NavigationContainer } from '@react-navigation/native';
import { createStackNavigator } from '@react-navigation/stack';
function HomeScreen() {
  return (
    <View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
      <Text>Home Screen</Text>
    </View>
  );
}
const Stack = createStackNavigator();
function App() {
  return (
    <NavigationContainer>
      <Stack.Navigator>
        <Stack.Screen name="Home" component={HomeScreen} />
      </Stack.Navigator>
    </NavigationContainer>
  );
}
export default App;
name prop을 받음component prop을 받음options를 특정할 수 있다<Stack.Screen
  name="Home"
  component={HomeScreen}
  options={{ title: 'Overview' }}
/>
screenOptions 사용하기React context 을 사용하고 context provider로 Navigator를 감싸기 (추천 방법)
render Callback 사용하기
<Stack.Screen name="Home">
  {props => <HomeScreen {...props} extraData={someData} />}
</Stack.Screen>
import * as React from 'react';
import { Button, View, Text } from 'react-native';
import { NavigationContainer } from '@react-navigation/native';
import { createStackNavigator } from '@react-navigation/stack';
function HomeScreen({ navigation }) {
  return (
    <View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
      <Text>Home Screen</Text>
      <Button
        title="Go to Details"
        onPress={**() => navigation.navigate('Details')**}
      />
    </View>
  );
}
navigationnavigation prop은 Stack Navigator에 있는 모든 Screen 컴포넌트에 보내짐
navigate('Details')navigation의 prop으로 navigate 함수 호출하기
function DetailsScreen({ navigation }) {
  return (
    <View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
      <Text>Details Screen</Text>
      <Button
        title="Go to Details... again"
        onPress={() => navigation.navigate('Details')}
      />
    </View>
  );
}
같은 screen으로 가고 싶을 때는?
navigate 대신 push 사용하기!navigate : 이 screen으로 go하기!
navigate를 호출하면 먼저 해당 이름의 기존 route를 찾고, stack에 아직 route가 없는 경우에만 새로운 route를 추가해줌
push : 이 screen을 add하기!
push를 호출하면 stack에 새 route를 추가함
navigation.goBack()
이전 screen으로 돌아가기
navigation.popToTop()
stack의 첫 screen으로 돌아가기
https://reactnavigation.org/docs/hello-react-navigation
https://reactnavigation.org/docs/navigating