- react navigation: https://reactnavigation.org/docs/getting-started/
- ๋ค์ ๋ฒ์ ์ ์ฌ์ฉํ์์ต๋๋ค.
"@react-navigation/native": "^6.0.11",
"@react-navigation/stack": "^6.2.2",
๋ฆฌ์กํธ ๋ค์ดํฐ๋ธ์์ ์ฌ์ฉํ๋ react-navigation์ stack ๋ฐฉ์์ ์ฌ์ฉํ๋ค.
stack ๋ฐฉ์์ ํด๋ฆญ์ ํ์๋ ํ๋ฉด์ด ํ๋์ฉ stack์ ์์ด๋๋ฐ ๊ทธ์ค ์ต์์ ๊ฒ์ ๋ณด์ฌ์ฃผ๋ ๋ฐฉ์์ด๋ค.
๊ทธ๋ฆฌ๊ณ back ๋ฒํผ์ ๋๋ฅด๋ฉด ์ต์๋จ์ ํ๋ฉด์ ์ ๊ฑฐํ๋ฏ๋ก์จ ์ด์ ํ๋ฉด์ ๋ณด์ฌ์ค๋ค.
* ์ค์น
expo install @react-navigation/native
expo install @react-navigation/stack
1. ํ๋ฉด ์ด๋ํ๊ธฐ
Home ๊ณผ Detail ํ์ด์ง ์ด๋ํ๊ธฐ
createStackNavigator() ๋ก Stack์ ์์ฑํ๋ค.
Stack.Navigator ์์ Stack.Screen์ ์ฌ์ฉํ์ฌ ์ฐ๊ฒฐํ screen์ ์์ฑํ๋ค.
Stack.screen์ name ์ URL์ ๋ฃ๊ณ component๋ฅผ ์ฐ๊ฒฐํ๋ค.
ํ๋ฉด์์ props์ผ๋ก navigation์ ๋ฐ์ ์ ์๋ค.
console๋ก ์ด๋ค ๊ฐ์ด ๋์ด์ค๋์ง ํ์ธํด๋ณด๋ฉด ๋ค์ํ ๊ธฐ๋ฅ๋ค์ด ์๋ค.
navigate(url) ์ ํตํ์ฌ url๋ก ์์ธํ์ด์ง๋ก ์ด๋ ํ ์ ์๊ณ ,
์์ธ ํ๋ฉด์์๋ goBack()์ ์ฌ์ฉํ์ฌ ๋ค์ Home ํ์ด์ง๋ก ๋์๊ฐ ์ ์๋ค.
์ฐธ๊ณ ๋ก navigate ๋์ push๋ฅผ ์ฌ์ฉํด๋ ์ข๋ค.
navigate์ goBack๋ ๊ฐ์ด ์ธ์ ์์ง๋ง, ์์ ๊ทธ๋ฆผ์ฒ๋ผ navigate๋ ์๋ก ํ์ด์ง๋ฅผ ์๋ก ์๋๋ฐฉ์์ผ ๊ฒ ๊ฐ๊ณ , goBack์ ์ต์๋จ ํ์ด์ง๋ฅผ ์์ ๋ ๋ฐฉ์์ผ ๊ฒ ๊ฐ๋ค. (์ ํํ ์์ค๋ฅผ ๋ถ์ํด๋ณด๊ฑฐ๋ ํ ์คํธ๋ฅผ ํด๋ณด์ง ์์์ 100% ํ์คํ์ง๋ ์๋ค!๐)
navigation >> Object {
"addListener": [Function addListener],
"canGoBack": [Function canGoBack],
"dispatch": [Function dispatch],
"getId": [Function getId],
"getParent": [Function getParent],
"getState": [Function anonymous],
"goBack": [Function anonymous],
"isFocused": [Function isFocused],
"navigate": [Function anonymous],
"pop": [Function anonymous],
"popToTop": [Function anonymous],
"push": [Function anonymous],
"removeListener": [Function removeListener],
"replace": [Function anonymous],
"reset": [Function anonymous],
"setOptions": [Function setOptions],
"setParams": [Function anonymous],
}
// HomeNavigator.js
import { createStackNavigator } from "@react-navigation/stack";
import { NavigationContainer } from '@react-navigation/native';
// components
import Home from "../screens/Home";
import ReviewDetails from '../screens/ReviewDetails'
const HomeStack = createStackNavigator()
export default function HomeNavigator() {
return(
<NavigationContainer>
<HomeStack.Navigator>
<HomeStack.Screen name="Home" component={Home} />
<HomeStack.Screen name="ReviewDetails" component={ReviewDetails} />
</HomeStack.Navigator>
</NavigationContainer>
)
}
// Home.js
import { StyleSheet, View, Text, Button } from 'react-native'
import React, { useState } from 'react'
import { globalStyles } from '../styles/global'
export default function Home({ navigation }) {
const pressHandler = () => {
navigation.navigate('ReviewDetails')
// navigation.push('ReviewDetails')
}
return (
<View style={globalStyles.container}>
<Text style={globalStyles.titleText}>Home Screen</Text>
<Button title='Go to review2' onPress={pressHandler} />
</View>
)
}
import { StyleSheet, View, Text, Button } from 'react-native'
import React, { useEffect } from 'react'
export default function ReviewDetails({navigation}) {
const pressHandler = () => {
// navigation.navigate('Home')
navigation.goBack()
}
return (
<View style={styles.container}>
<Button title="Go back to Home" onPress={pressHandler} />
</View>
)
}
const styles = StyleSheet.create({
container: {
padding: 24
}
})
2. ํ์ด์ง์์ ํ๋ผ๋ฏธํฐ๋ก ๋ฐ์ดํฐ ๋ฐ๊ธฐ
ํ๋ฉด์์ props์ผ๋ก navigation๋ฅผ ๋ฐ์์ ํ์ด์ง ์ด๋ํ๋ ํจ์๋ฅผ ์ธ ์ ์์๋๋ฐ,
๋น์ทํ๊ฒ route๋ฅผ props์ผ๋ก ๋ฐ์์ data๋ฅผ ์ ๋ฌ ํ ์๋ ์๋ค.
FlatList ์ item์ ํด๋ฆญ์ ์์ธ ํ์ด์ง๋ก ์ด๋ํ๋ฉด์ ๋ฐ์ดํฐ๋ฅผ ๊ฐ์ด ๋๊ฒจ ๋ณด์๋ค.
// Home.js
import { StyleSheet, View, Text, Button, FlatList, TouchableOpacity } from 'react-native'
import React, { useState } from 'react'
import { globalStyles } from '../styles/global'
export default function Home({ navigation }) {
const [reviews, setReviews] = useState([
{ title: 'Apple', rating: 5, body: 'Apple is yam', key: '1' },
{ title: 'Banana', rating: 3, body: 'Banana is sweet', key: '2' },
{ title: 'Orange', rating: 2, body: 'Orange is great', key: '3' },
])
const pressHandler = () => {
navigation.navigate('ReviewDetails')
// navigation.push('ReviewDetails')
}
return (
<View style={globalStyles.container}>
<Text style={globalStyles.titleText}>Home Screen</Text>
<Button title='Go to review2' onPress={pressHandler} />
<FlatList
data={reviews}
renderItem={({ item }) => (
// navigate('link', obj) 2๋ฒ์งธ ํ๋ผ๋ฏธํฐ๋ก ์ค๋ธ์ ํธ๋ฅผ ๋ณด๋ผ์ ์๋ค.
<TouchableOpacity onPress={() => navigation.navigate('ReviewDetails', item)}>
<Text
style={globalStyles.titleText}
>{item.title}</Text>
</TouchableOpacity>
)}
/>
</View>
)
}
import { StyleSheet, View, Text, Button } from 'react-native'
import React, { useEffect } from 'react'
export default function ReviewDetails({route, navigation}) {
const {title, body, rating} = route.params
const pressHandler = () => {
// navigation.navigate('Home')
navigation.goBack()
}
return (
<View style={styles.container}>
<Text>{title}</Text>
<Text>{body}</Text>
<Text>{rating}</Text>
<Button title="Go back to Home" onPress={pressHandler} />
</View>
)
}
const styles = StyleSheet.create({
container: {
padding: 24
}
})
๋ค์๊ณผ ๊ฐ์ด ๊ตฌํํ ์ ์๋ค!
* ๋ฒ์ธ
๊ทธ๋ฆฌ๊ณ ๊ฐ์๋ฅผ ๋ณด๋ค๋ณด๋ฉด
๊ทธ๋ฆฌ๊ณ react-navigation/stack ๋ฅผ ๊ฐ์ด ์ค์นํด ์ฃผ๋๋ฐ, ์ด๋ ๊ณต์๋ฌธ์์ ๋ฐ๋ฅด๋ฉด stack์ ์ต์์ ์๋ ์ปดํฌ๋ํธ๊ฐ์ ์คํฌ๋ฆฐ ์ ํ์ ์ํด ์ฌ์ฉ๋๋ ๋ฏํ๋ค. (Stack Navigator provides a way for your app to transition between screens where each new screen is placed on top of a stack.)
- react-navigation/stack : https://reactnavigation.org/docs/stack-navigator/
'๊ฐ๋ฐ ๐พ > React Native' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
screenOptions๋ก ํค๋ ๊พธ๋ฏธ๊ธฐ (0) | 2022.07.30 |
---|---|
[expo] ์นํฐํธ webfont ์ ์ฉํ๊ธฐ (0) | 2022.07.18 |
TextInput ์ ๋ ฅ์ Keyboard ์ฐฝ ๋ซ๊ธฐ (0) | 2022.07.16 |
expo๋ก ์์ํ ๋ ๊ธฐ๋ณธ ์ค์ง ๋ชฉ๋ก ์ ๋ฆฌ (0) | 2022.07.09 |