React Navigation 4
리액트 네비게이션 마스터하기!!
그냥 배우면서 쓰려고했는데 너무 어려워 한번 제대로 정리해보려 한다..
createStackNavigator
Options for the router:
- initialRouteName : 스택의 가장 기본화면 설정, 경로의 키와 반드시 일치해야한다
- navigationOptions : 네비게이터 자체의 옵션, 부모 네비게이터를 형성시킨다
- defaultNavigationOptions : 사용하는 스크린들의 기본옵션
- paths :
import React from 'react';
import {
  ActivityIndicator,
  AsyncStorage,
  Button,
  StatusBar,
  StyleSheet,
  View,
} from 'react-native';
import { createStackNavigator, createSwitchNavigator, createAppContainer } from 'react-navigation';
class SignInScreen extends React.Component {
  static navigationOptions = {
    title: 'Please sign in',
  };
  render() {
    return (
      <View style={styles.container}>
        <Button title="Sign in!" onPress={this._signInAsync} />
      </View>
    );
  }
  _signInAsync = async () => {
    await AsyncStorage.setItem('userToken', 'abc');
    this.props.navigation.navigate('App');
  };
}
class HomeScreen extends React.Component {
  static navigationOptions = {
    title: 'Welcome to the app!',
  };
  render() {
    return (
      <View style={styles.container}>
        <Button title="Show me more of the app" onPress={this._showMoreApp} />
        <Button title="Actually, sign me out :)" onPress={this._signOutAsync} />
      </View>
    );
  }
  _showMoreApp = () => {
    this.props.navigation.navigate('Other');
  };
  _signOutAsync = async () => {
    await AsyncStorage.clear();
    this.props.navigation.navigate('Auth');
  };
}
class OtherScreen extends React.Component {
  static navigationOptions = {
    title: 'Lots of features here',
  };
  render() {
    return (
      <View style={styles.container}>
        <Button title="I'm done, sign me out" onPress={this._signOutAsync} />
        <StatusBar barStyle="default" />
      </View>
    );
  }
  _signOutAsync = async () => {
    await AsyncStorage.clear();
    this.props.navigation.navigate('Auth');
  };
}
class AuthLoadingScreen extends React.Component {
  constructor() {
    super();
    this._bootstrapAsync();
  }
  // Fetch the token from storage then navigate to our appropriate place
  _bootstrapAsync = async () => {
    const userToken = await AsyncStorage.getItem('userToken');
    // This will switch to the App screen or Auth screen and this loading
    // screen will be unmounted and thrown away.
    this.props.navigation.navigate(userToken ? 'App' : 'Auth');
  };
  // Render any loading content that you like here
  render() {
    return (
      <View style={styles.container}>
        <ActivityIndicator />
        <StatusBar barStyle="default" />
      </View>
    );
  }
}
const styles = StyleSheet.create({
  container: {
    flex: 1,
    alignItems: 'center',
    justifyContent: 'center',
  },
});
const AppStack = createStackNavigator({ Home: HomeScreen, Other: OtherScreen });
const AuthStack = createStackNavigator({ SignIn: SignInScreen });
export default createAppContainer(createSwitchNavigator(
  {
    AuthLoading: AuthLoadingScreen,
    App: AppStack,
    Auth: AuthStack,
  },
  {
    initialRouteName: 'AuthLoading',
  }
));
'코드스테이츠(Immersive) > 4주프로젝트' 카테고리의 다른 글
| 4주프로젝트 (Hollyship_day09 : spotify API 가져오기) (0) | 2019.09.26 | 
|---|---|
| 4주프로젝트 (Hollyship_day06 : TypeScript and React) (0) | 2019.09.23 | 
| 4주프로젝트 (Hollyship_day04 : React navigation) (0) | 2019.09.19 | 
| 4주프로젝트 (Hollyship_day03 : 프론트 목업 제작 및 코딩시작) (0) | 2019.09.18 | 
| 4주프로젝트 (Hollyship_day02 : data schema) (0) | 2019.09.17 |