[React Native] Navigation์ ์ด์ฉํ ํ๋ฉด ์ ํ / ๋ค์ดํฐ๋ธ ์คํ·ํ๋จ ํญ ๋ค๋น๊ฒ์ดํฐ ํจ๊ป ๊ตฌํ
๊ตฌํ ํ๊ฒฝ
Mac OS, ARM(M1)
React-native CLI
version 0.68.2
์ค์น
$ cd [ํ๋ก์ ํธ๋ช
]
$ yarn add @react-navigation/native
$ yarn add react-native-screens react-native-safe-area-context
$ cd ios
$ pod install
๋ค๋น๊ฒ์ดํฐ์ ์ต์ํ์ง ์์์ ์ฒ์์ ์คํ์ ๋ถ๋ฆฌํด์ ์์ฑํ๋ค.
App.js์์ ๋ค๋น๊ฒ์ดํฐ๋ฅผ ์ฌ์ฉํ๊ณ ,
MainScreen.js์์ ํ๋จํญ ๋ค๋น๊ฒ์ดํฐ๋ฅผ ์ฌ์ฉํ๋ค.
๋ค๋น๊ฒ์ดํฐ๊ฐ ๋ถ๋ฆฌ๋์ด ์์ด ๊ตฌ์กฐ๊ฐ ํ ๋์ ๋ณด์ด์ง ์์์ ๋ฐ๊พธ๊ณ ์ถ์๋ค.
App.js (์์ ์ )
import * as React from 'react';
import { NavigationContainer } from '@react-navigation/native';
import { createNativeStackNavigator } from '@react-navigation/native-stack';
import MainScreen from './android/screens/MainScreen'
import LikeScreen from './android/screens/LikeScreen'
import Cafe from './android/screens/Category/Cafe'
import Restaurant from './android/screens/Category/Restaurant'
import Drink from './android/screens/Category/Drink'
import Etc from './android/screens/Category/Etc'
import Random from './android/screens/Category/Random';
const Stack = createNativeStackNavigator();
export default function App() {
return (
<NavigationContainer>
<Stack.Navigator>
<Stack.Screen
name="Main"
component={MainScreen}
options={{headerShown: false}}
/>
<Stack.Screen
name="Like"
component={LikeScreen}
/>
<Stack.Screen
name="Cafe"
component={Cafe}
/>
<Stack.Screen
name="Restaurant"
component={Restaurant}
/>
<Stack.Screen
name="Drink"
component={Drink}
/>
<Stack.Screen
name="Etc"
component={Etc}
/>
<Stack.Screen
name="Random"
component={Random}
/>
</Stack.Navigator>
</NavigationContainer>
);
}
MainScreen.js (์์ ์ )
import * as React from 'react';
import {Text, TextInput, View, Button, Image, StyleSheet, ScrollView, TouchableOpacity} from 'react-native'
import { createBottomTabNavigator } from '@react-navigation/bottom-tabs';
import Icon from 'react-native-vector-icons/MaterialIcons'
import { useNavigation } from '@react-navigation/native';
import MapScreen from './MapScreen';
import CourseScreen from './CourseScreen';
import MyPageScreen from './MyPageScreen';
const Tab = createBottomTabNavigator();
function MainScreen(){
return (
<Tab.Navigator initialRouteName="Home" screenOptions={{tabBarActiveTintColot: '#fd8c00'}}>
<Tab.Screen name="Home" component={HomeScreen} options={{
title: 'Home',
tabBarIcon: ({color, size}) => (
<Icon name="home" color={color} size={size} />
),
}}/>
<Tab.Screen name="Map" component={MapScreen} options={{
title: 'Map',
tabBarIcon: ({color, size}) => (
<Icon name="map" color={color} size={size} />
),
}}/>
<Tab.Screen name="Course" component={CourseScreen} options={{
title: 'Course',
tabBarIcon: ({color, size}) => (
<Icon name="like" color={color} size={size} />
),
}}/>
<Tab.Screen name="My Page" component={MyPageScreen} options={{
title: 'My Page',
tabBarIcon: ({color, size}) => (
<Icon name="settings" color={color} size={size} />
),
}}/>
</Tab.Navigator>
);
}
function LikeButton() {
const navigation = useNavigation();
return (
<TouchableOpacity activeOpacity={0.8} onPress={() => navigation.push('Like', { id: 1, })}>
<Text style={{fontSize: 18, marginHorizontal: 10, marginVertical: 10}}>โค๏ธ</Text>
</TouchableOpacity>
);
}
function CafeButton() {
const navigation = useNavigation();
return (
<TouchableOpacity activeOpacity={0.8} onPress={() => navigation.push('Cafe', { id: 2, })}>
<Text style={{fontSize: 40, marginHorizontal: 20, marginVertical: 10}}>๐ง</Text>
<Text style={{fontSize: 16, marginHorizontal: 30}}>Cafe</Text>
</TouchableOpacity>
);
}
function RestaurantButton() {
const navigation = useNavigation();
return (
<TouchableOpacity activeOpacity={0.8} onPress={() => navigation.push('Restaurant', { id: 3, })}>
<Text style={{fontSize: 40, marginHorizontal: 23, marginVertical: 10}}>๐ด</Text>
<Text style={{fontSize: 16, marginHorizontal: 8}}>Restaurant</Text>
</TouchableOpacity>
);
}
function DrinkButton() {
const navigation = useNavigation();
return (
<TouchableOpacity activeOpacity={0.8} onPress={() => navigation.push('Drink', { id: 4, })}>
<Text style={{fontSize: 40, marginHorizontal: 20, marginVertical: 10}}>๐บ</Text>
<Text style={{fontSize: 16, marginHorizontal: 25}}>Drink</Text>
</TouchableOpacity>
);
}
function EtcButton() {
const navigation = useNavigation();
return (
<TouchableOpacity activeOpacity={0.8} onPress={() => navigation.push('Etc', { id: 5, })}>
<Text style={{fontSize: 40, marginHorizontal: 20, marginVertical: 10}}>๐</Text>
<Text style={{fontSize: 16, marginHorizontal: 33}}>Etc</Text>
</TouchableOpacity>
);
}
function RandomImage() {
const navigation = useNavigation();
return (
<TouchableOpacity activeOpacity={0.8} onPress={() => navigation.push('Random', { id: 6, })}>
<Image style={styles.place} source={require('../assets/logo.png')}/>
<View style={styles.info}>
<Text>์ ๋ชฉ</Text>
<Text>์ค๋ช
๋ธ๋ผ๋ธ๋ผ</Text>
</View>
</TouchableOpacity>
);
}
function HomeScreen() {
return(
<View style={{flex: 1, flexDirection: 'column', backgroundColor: 'white'}}>
<View style={styles.searchBar}>
<Image style={styles.logoStyle} source={require('../assets/logo.png')}/>
<TextInput style={styles.input}
placeholder="#๊ฒ์์ด๋ฅผ ์
๋ ฅํ์ธ์."
autoCapitalize="none" />
<TouchableOpacity activeOpacity={0.8}>
<Text style={{fontSize: 18, marginHorizontal: 5}}>๐</Text>
</TouchableOpacity>
<LikeButton style={styles.buttonStyle}/>
</View>
<View style={styles.locationBar}>
<Text>์ฉ์ฐ๊ตฌ ์ฒญํ๋ก 47๊ธธ 100 ๐</Text>
</View>
<View style={styles.categoryBar}>
<CafeButton style={styles.category}/>
<RestaurantButton style={styles.category}/>
<DrinkButton style={styles.category}/>
<EtcButton style={styles.category}/>
</View>
<View style={{flex: 0.4, margin: 15}}>
<Text style={styles.randomText}>๋ฌด์์ ์ถ์ฒ</Text>
</View>
<View style={styles.randomBar}>
<ScrollView horizontal={false} style={styles.list}>
<View style={styles.stylegridView}>
<RandomImage/>
<RandomImage/>
<RandomImage/>
<RandomImage/>
<RandomImage/>
<RandomImage/>
</View>
</ScrollView>
</View>
</View>
);
}
const styles = StyleSheet.create({
์ดํ ์๋ต
});
export default MainScreen;
MainScreen์ ํํ๋ฉด UI๋ฅผ ๋ง๋ค์ด๋๋๋ฐ,
ํ๊ณผ ๋ฉ์ธ์ ๊ฐ์ ํ๋ฉด์ผ๋ก ํท๊ฐ๋ ค์ ์ฝ๋๋ฅผ ์์ฒ๋ผ ์์ฑํ ๊ฒ ๊ฐ๋ค.
์ด๋ ๊ฒ ํ๋ฉด์ ๊ตฌ์ฑํ๋ฉด ํํ๋ฉด์์
<Like, Cafe, Restaurant, Drink, Etc, Random>
ํ์ด์ง๋ก ํ๋ฉด์ ์ด๋ํ ์๋ ์์ง๋ง
๋๋จธ์ง ํ๋จํญ์ธ ์ง๋, ์ฝ์ค, ๋ง์ดํ์ด์ง์ ๊ฐ ํ๋ฉด์์
๊ทธ ๋ด๋ถ์ ๋ค๋ฅธ ํ์ด์ง๋ก ์ด๋์ด ๋ถ๊ฐ๋ฅํ๋ค.
์ด์ฐจํผ ๋์ค์ ํ์๋ค๋ผ๋ฆฌ ๋ง๋ ํ๋ฉด์ ์๋ก ์ฐ๊ฒฐํด์ผ ํ๊ธฐ ๋๋ฌธ์
๋ด๊ฐ ์ ์ฒด ๋ค๋น๊ฒ์ดํฐ ๋ฐฉํฅ์ฑ์ ์ก๊ณ
๋ด ๊ฑธ ํ์๋ค์๊ฒ ์ ๋ฌํ๋๊ฒ ํ์ ์ ์ข์ ๊ฒ ๊ฐ์์
App.js์ ์ ์ฒด์ ์ธ js ํ์ผ ๊ตฌ์กฐ๋ค์ ๋ฐ๊ฟจ๋ค.
๋ค๋น๊ฒ์ด์ ์ ๋จธ๋ฆฟ์์ผ๋ก๋ง ๊ตฌ์ํ๋ ํท๊ฐ๋ ค์
์๋์ฒ๋ผ ๊ตฌ์ฑ๋๋ฅผ ๊ทธ๋ ค์ App.js์ ๋ฐ์ํ๋ค.
๊ฐ๊ฒ ํ์ด์ง๋ค์ ์ดํ์ ๋ฐฑ์๋์ ์ฐ๋ํด์ผ ํด์
์ด์ ์ ๊ฐ๊ฐ <Cafe, Restaurant, Drink, Etc> ํ์ด์ง๋ฅผ ๋ง๋ค์ด๋๋๋ฐ
๊ทธ๊ฑธ ์ญ์ ํ๊ณ ๋น Place ํ์ด์ง ํ๋๋ก ํต์ผํด์ ์ด๋ํ๊ฒ ํ๋ค.
๋ก๊ทธ์ธ ํ๋ฉด์ ๋ค๋ฅธ ํ์์ด ์์ ์ค์ด๋ผ
๋น ํ๋ฉด์ ๋ฒํผ ํ๋๋ฅผ ๋๊ณ
๋ฒํผ์ ํด๋ฆญํ๋ฉด ๋ฉ์ธ์ผ๋ก ์ด๋ํ๋๋ก ํ๋ค.
MainScreen์ ์์ฑํ๋ ํ๋จํญ ๋ค๋น๊ฒ์ดํฐ๋ฅผ App.js๋ก ํฉ์น๊ณ ,
Main์์ <Like, Cafe, Restaurant, Drink, Etc, Random>
๊ฐ ํ์ด์ง๋ก ์ด๋ํ ์ ์๋๋ก ์์ฑํ๋ค.
App.js (์์ ํ)
import * as React from 'react';
import { NavigationContainer } from '@react-navigation/native';
import { createNativeStackNavigator } from '@react-navigation/native-stack';
import { createBottomTabNavigator } from '@react-navigation/bottom-tabs';
import Icon from 'react-native-vector-icons/MaterialIcons'
import Login from './android/screens/Login/Login'
import HomeScreen from './android/screens/HomeScreen';
import LikeScreen from './android/screens/LikeScreen'
import PlaceScreen from './android/screens/PlaceScreen';
import MapScreen from './android/screens/MapScreen';
import CourseScreen from './android/screens/CourseScreen';
import MyPageScreen from './android/screens/MyPageScreen';
const Stack = createNativeStackNavigator();
const Tab = createBottomTabNavigator();
function Main(){
return (
<Tab.Navigator initialRouteName="Home" screenOptions={{tabBarActiveTintColot: '#fd8c00'}}>
<Tab.Screen name="Home" component={HomeScreen} options={{
title: 'Home',
tabBarIcon: ({color, size}) => (
<Icon name="Home" color={color} size={size} />
),
headerShown: false
}}/>
<Tab.Screen name="Map" component={MapScreen} options={{
title: 'Map',
tabBarIcon: ({color, size}) => (
<Icon name="Place" color={color} size={size} />
),
}}/>
<Tab.Screen name="Course" component={CourseScreen} options={{
title: 'Course',
tabBarIcon: ({color, size}) => (
<Icon name="Check" color={color} size={size} />
),
}}/>
<Tab.Screen name="My Page" component={MyPageScreen} options={{
title: 'My Page',
tabBarIcon: ({color, size}) => (
<Icon name="Person" color={color} size={size} />
),
}}/>
</Tab.Navigator>
);
}
export default function App() {
return (
<NavigationContainer>
<Stack.Navigator initialRouteName="Login">
<Stack.Screen
name="Sign In"
component={Login}
/>
<Stack.Screen
name="Main"
component={Main}
options={{headerShown: false}}
/>
<Stack.Screen
name="Like"
component={LikeScreen}
/>
<Stack.Screen
name="Cafe"
component={PlaceScreen}
/>
<Stack.Screen
name="Restaurant"
component={PlaceScreen}
/>
<Stack.Screen
name="Drink"
component={PlaceScreen}
/>
<Stack.Screen
name="Etc"
component={PlaceScreen}
/>
<Stack.Screen
name="Random"
component={PlaceScreen}
/>
</Stack.Navigator>
</NavigationContainer>
);
}
HomeScreen.js (์์ ํ)
import * as React from 'react';
import {Text, TextInput, View, Image, StyleSheet, ScrollView, TouchableOpacity} from 'react-native'
import { useNavigation } from '@react-navigation/native';
function LikeButton() {
const navigation = useNavigation();
return (
<TouchableOpacity activeOpacity={0.8} onPress={() => navigation.push('Like')}>
<Text style={{fontSize: 18, marginHorizontal: 10, marginVertical: 10}}>โค๏ธ</Text>
</TouchableOpacity>
);
}
function CafeButton() {
const navigation = useNavigation();
return (
<TouchableOpacity activeOpacity={0.8} onPress={() => navigation.push('Cafe')}>
<Text style={{fontSize: 40, marginHorizontal: 20, marginVertical: 10}}>๐ง</Text>
<Text style={{fontSize: 16, marginHorizontal: 30}}>Cafe</Text>
</TouchableOpacity>
);
}
function RestaurantButton() {
const navigation = useNavigation();
return (
<TouchableOpacity activeOpacity={0.8} onPress={() => navigation.push('Restaurant')}>
<Text style={{fontSize: 40, marginHorizontal: 23, marginVertical: 10}}>๐ด</Text>
<Text style={{fontSize: 16, marginHorizontal: 8}}>Restaurant</Text>
</TouchableOpacity>
);
}
function DrinkButton() {
const navigation = useNavigation();
return (
<TouchableOpacity activeOpacity={0.8} onPress={() => navigation.push('Drink')}>
<Text style={{fontSize: 40, marginHorizontal: 20, marginVertical: 10}}>๐บ</Text>
<Text style={{fontSize: 16, marginHorizontal: 25}}>Drink</Text>
</TouchableOpacity>
);
}
function EtcButton() {
const navigation = useNavigation();
return (
<TouchableOpacity activeOpacity={0.8} onPress={() => navigation.push('Etc')}>
<Text style={{fontSize: 40, marginHorizontal: 20, marginVertical: 10}}>๐</Text>
<Text style={{fontSize: 16, marginHorizontal: 33}}>Etc</Text>
</TouchableOpacity>
);
}
function RandomImage() {
const navigation = useNavigation();
return (
<TouchableOpacity activeOpacity={0.8} onPress={() => navigation.push('Random')}>
<Image style={styles.place} source={require('../assets/logo.png')}/>
<View style={styles.info}>
<Text>์ ๋ชฉ</Text>
<Text>์ค๋ช
๋ธ๋ผ๋ธ๋ผ</Text>
</View>
</TouchableOpacity>
);
}
function HomeScreen() {
return(
<View style={{flex: 1, flexDirection: 'column', backgroundColor: 'white'}}>
<View style={styles.searchBar}>
<Image style={styles.logoStyle} source={require('../assets/logo.png')}/>
<TextInput style={styles.input}
placeholder="#๊ฒ์์ด๋ฅผ ์
๋ ฅํ์ธ์."
autoCapitalize="none" />
<TouchableOpacity activeOpacity={0.8}>
<Text style={{fontSize: 18, marginHorizontal: 5}}>๐</Text>
</TouchableOpacity>
<LikeButton style={styles.buttonStyle}/>
</View>
<View style={styles.locationBar}>
<Text>์ฉ์ฐ๊ตฌ ์ฒญํ๋ก 47๊ธธ 100 ๐</Text>
</View>
<View style={styles.categoryBar}>
<CafeButton style={styles.category}/>
<RestaurantButton style={styles.category}/>
<DrinkButton style={styles.category}/>
<EtcButton style={styles.category}/>
</View>
<View style={{flex: 0.4, margin: 15}}>
<Text style={styles.randomText}>๋ฌด์์ ์ถ์ฒ</Text>
</View>
<View style={styles.randomBar}>
<ScrollView horizontal={false} style={styles.list}>
<View style={styles.stylegridView}>
<RandomImage/>
<RandomImage/>
<RandomImage/>
<RandomImage/>
<RandomImage/>
<RandomImage/>
</View>
</ScrollView>
</View>
</View>
);
}
const styles = StyleSheet.create({
์ดํ ์๋ต
});
export default HomeScreen;
์ด์ ์ ๋นํด ์ฝ๋๊ฐ ํจ์ฌ ๊น๋ํด์ก๊ณ ,
๋ก๊ทธ์ธ-๋ฉ์ธ, ๋ฉ์ธ-๊ฐ ํ์ด์ง๋ก์ ์ฐ๊ฒฐ๋ ๊ฐ๋ฅํ๋ค.
์์ฑ ํ๋ฉด์ ๋ค์๊ณผ ๊ฐ๋ค.
๋ก๊ทธ์ธ - ๋ฉ์ธ - Cafe