๊ฐœ๋ฐœ ๐Ÿพ/React Native

React Router

JOTOKKI 2022. 7. 19. 23:41
728x90

- 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/

 

 

 

 

 

 

 

๋ฐ˜์‘ํ˜•