로메오의 블로그

[React Native] Stack navigation 코드 분리하기 본문

App & OS/Hybrid

[React Native] Stack navigation 코드 분리하기

romeoh 2019. 6. 23. 21:46
반응형

App.js

import React, { Component } from 'react';
import { createAppContainer, createStackNavigator } from 'react-navigation'

import HomeScreen from './src/HomeScreen'
import DetailsScreen from './src/DetailScreen'

const RootStack = createStackNavigator(
    {
        Home: {
            screen: HomeScreen,
        },
        Details: {
            screen: DetailsScreen,
        },
    },
    {
        initialRouteName: 'Home',
    }
);

const AppContainer = createAppContainer(RootStack);

export default class App extends React.Component {
    render() {
        return <AppContainer />;
    }
}

 

./src/HomeScreen.js

$ touch src/HomeScreen.js
import React, { Component } from 'react';
import { View, Text, Button } from 'react-native';

export default class HomeScreen extends 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>
		);
	}
}

 

./src/DetailScreen.js

$ touch src/DetailScreen.js
import React, { Component } from 'react';
import { View, Text, Button } from 'react-native';

export default class HomeScreen extends Component {
	render() {
		return (
			<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
                <Text>Details Screen</Text>
                <Button
                    title="Go to Details... again"
                    onPress={() => this.props.navigation.push('Details')}
                />
                <Button
                    title="Go to Home"
                    onPress={() => this.props.navigation.navigate('Home')}
                />
                <Button
                    title="Go back"
                    onPress={() => this.props.navigation.goBack()}
                />
            </View>
		);
	}
}

 

iOS App Build

$ react-native run-ios

 

Android App Build

$ react-native run-android

 

 

반응형
Comments