React Navigation 5 Tutorials [ #6 ]

์กฐํŒ”๋กœยท2020๋…„ 10์›” 3์ผ
1

React-Navigation-5 Tutorials

๋ชฉ๋ก ๋ณด๊ธฐ
6/6
post-thumbnail

React Navigation 5 Tutorials

๐Ÿ“Œ ํŠœํ† ๋ฆฌ์–ผ์„ ๋”ฐ๋ผํ•œ ํ”„๋กœ์ ํŠธ์ž…๋‹ˆ๋‹ค!

ํ•˜๋‹จ๋ฐ” ์ƒ์„ฑํ•˜๊ธฐ

์ด๊ณณ์„ ์ฐธ๊ณ ํ–ˆ์Šต๋‹ˆ๋‹ค!

๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์„ค์น˜ํ•˜๊ธฐ

installํ•˜๊ธฐ

npm install @react-navigation/bottom-tabs react-native-paper
npm install @react-navigation/material-bottom-tabs

ExploreScreen & MainTabScreen ์ƒ์„ฑํ•˜๊ธฐ

ExploreScreen.js

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

const ExploreScreen = () => {
    return(
        <View style={styles.container}>
            <Text>ExploreScreen</Text>
            <Button
                title="Click Here"
                onPress={()=>alert("Button Clicked!")} 
            />
        </View>
    );
};

const styles = StyleSheet.create({
    container : {
        flex: 1,
        alignItems: 'center',
        justifyContent: 'center',
    },
})
export default ExploreScreen;

MainTabScreen.js

App.js์— ์žˆ๋Š” DetailsStackScreen, HomeStackScreen ๋ถ€๋ถ„์„ ๋ณต๋ถ™ํ•ด์ค์‹œ๋‹ค!

import React from 'react';
import { createStackNavigator } from '@react-navigation/stack';

import Icon from 'react-native-vector-icons/Ionicons';

import HomeScreen from './HomeScreen';
import DetailsScreen from './DetailsScreen';

const HomeStack = createStackNavigator();
const DetailsStack = createStackNavigator();

const HomeStackScreen = ({navigation}) => {
    return (
      
    );
};

const DetailsStackScreen = ({navigation}) => {
    return (
        <DetailsStack.Navigator screenOptions={{
            headerStyle:{
                backgroundColor: '#009387',
            },
            headerTintColor: '#fff',
            headerTitleStyle: {
                fontWeight: 'bold',
            }
        }}>
        <DetailsStack.Screen name="Details" component={DetailsScreen} options={{
            title : 'Details',
            headerLeft: () => (
                <Icon.Button name='ios-menu' size={25}
                backgroundColor= '#009387' onPress = {() => {navigation.openDrawer()}}>
                </Icon.Button>
            )
        }}/>
        </DetailsStack.Navigator>
    );};

์œ„์˜ docs์•ˆ์— ์˜ˆ์ œ ๋ถ€๋ถ„์„ ๋ณต๋ถ™ํ•ด์ค์‹œ๋‹ค! + Home / Details / Profile / Explore๋กœ ๋ณ€๊ฒฝํ•ด์ค์‹œ๋‹ค!

์ „์ฒด ์ฝ”๋“œ

import React, {useState, useEffect} from 'react';
import {StyleSheet} from 'react-native';
import { createStackNavigator } from '@react-navigation/stack';
import { createMaterialBottomTabNavigator } from '@react-navigation/material-bottom-tabs';

import Icon from 'react-native-vector-icons/Ionicons';
import MaterialCommunityIcons from 'react-native-vector-icons/MaterialCommunityIcons';

import HomeScreen from './HomeScreen';
import DetailsScreen from './DetailsScreen';
import ExploreScreen from './ExploreScreen';
import ProfileScreen from './ProfileScreen';

const HomeStack = createStackNavigator();
const DetailsStack = createStackNavigator();

const Tab = createMaterialBottomTabNavigator();

const MainTabScreen = () => {

    return (
        <Tab.Navigator
            initialRouteName="Home"
            tabBarOptions={{
                activeTintColor: '#fff',
                tabStyle: {
                    backgroundColor: '#009387',
                }
                
            }}
        >

            <Tab.Screen
                name="Home"
                component={HomeStackScreen}
                options={{
                    tabBarLabel: 'Home',
                    tabBarColor: '#009387',
                    tabBarIcon: ({ color, size }) => (
                    <MaterialCommunityIcons  
                        name="home"  
                        color={color} 
                        size={26} 
                    />
                    ),
                }}
            />

            <Tab.Screen
                name="Details"
                component={DetailsStackScreen}
                options={{
                    tabBarLabel: 'Details',
                    tabBarColor: '#1f65ff',
                    tabBarIcon: ({ color, size }) => (
                        <MaterialCommunityIcons
                            name="bell-outline" 
                            color={color} 
                            size={26} 
                        />
                    ),
                    tabBarBadge: 3,
                }}
            />

            <Tab.Screen
                name="Profile"
                component={ProfileScreen}
                options={{
                    tabBarLabel: 'Profile',
                    tabBarColor: '#694fad',
                    tabBarIcon: ({ color, size }) => (
                    <MaterialCommunityIcons  
                        name="account"  
                        color={color} 
                        size={26} 
                    />
                    ),
                }}
            />

            <Tab.Screen
                name="Explore"
                component={ExploreScreen}
                options={{
                    tabBarLabel: 'Explore',
                    tabBarColor: '#d02860',
                    tabBarIcon: ({ color, size }) => (
                    <MaterialCommunityIcons  
                        name="google-chrome"  
                        color={color} 
                        size={26} 
                    />
                    ),
                }}
            />
        </Tab.Navigator>
    );
}

const HomeStackScreen = ({navigation}) => {
    return (
        <HomeStack.Navigator screenOptions={{
            headerStyle:{
                backgroundColor: '#009387',
            },
            headerTintColor: '#fff',
            headerTitleStyle: {
                fontWeight: 'bold',
            }       
        }}>
        <HomeStack.Screen name="Home" component={HomeScreen} options={{
            title : 'Home',
            headerLeft: () => (
                <Icon.Button name='ios-menu' size={25}
                backgroundColor= '#009387' onPress = {() => {navigation.openDrawer()}} />
            )
        }}/>
        </HomeStack.Navigator>
    );
};

const DetailsStackScreen = ({navigation}) => {
    return (
        <DetailsStack.Navigator screenOptions={{
            headerStyle:{
                backgroundColor: '#1f65ff',
            },
            headerTintColor: '#fff',
            headerTitleStyle: {
                fontWeight: 'bold',
            }
        }}>
        <DetailsStack.Screen name="Details" component={DetailsScreen} options={{
            title : 'Details',
            headerLeft: () => (
                <Icon.Button 
                    name='ios-menu' 
                    size={25}
                    backgroundColor= '#1f65ff' 
                    onPress = {() => {navigation.openDrawer()}} 
                />
            )
        }}/>
        </DetailsStack.Navigator>
    );
};

export default MainTabScreen;

๊ฒฐ๊ณผํ™•์ธ

profile
ํ˜„์‹ค์— ์•ˆ์ฃผํ•˜์ง€ ์•Š๊ณ  - ๊ฐœ๋ฐœ์ž

0๊ฐœ์˜ ๋Œ“๊ธ€

๊ด€๋ จ ์ฑ„์šฉ ์ •๋ณด