Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | |||||
3 | 4 | 5 | 6 | 7 | 8 | 9 |
10 | 11 | 12 | 13 | 14 | 15 | 16 |
17 | 18 | 19 | 20 | 21 | 22 | 23 |
24 | 25 | 26 | 27 | 28 | 29 | 30 |
31 |
Tags
- 페이지리렌더링
- fcm 푸시
- 자동로그인
- Icon
- 리액트
- algorithm
- modal
- 모달
- vision-camera
- 2023년 정처기 필기
- touchableopacity
- API
- 순열
- 리액트네이티브
- TextInput
- JSON
- 모달에서뒤로가기
- makePermutation
- 가격자릿수
- 알고리즘
- asyncstorage
- Flatlist
- C++98에러
- ReactNative
- react-native
- 뒤로가기구현
- fcm push
- 재귀함수
- 리액트 네이티브
- React
Archives
- Today
- Total
생각은 길게 코딩은 짧게
[React-Native] React Navigation 본문
728x90
안녕하세요 세히이 입니다 이번에는 네비게이션을 적용해보았답니다!
Stack Navigator
1) 리액트 네비게이션 라이브러리 & 추가 라이브러리 설치
npm install @react-navigation/native
#아래도 필수이므로 설치해주세요!
npm install react-native-reanimated react-native-gesture-handler react-native-screens react-native-safe-area-context @react-native-community/masked-view
2) Stack 을 위한 라이브러리 설치
npm install @react-navigation/native-stack
3) 필요한 component 를 import 시켜주기 <App.js>
import React, { Component } from "react";
import {View, Text, FlatList} from 'react-native';
import { NavigationContainer} from '@react-navigation/native';
import { createNativeStackNavigator } from '@react-navigation/native-stack';
//경로를 위한 import
import Product from "./components/Product";
import UploadProduct from "./components/UploadProduct";
4) 경로 지정해주기 <App.js>
const Stack=createNativeStackNavigator();
class App extends Component{
render(){
return (
<NavigationContainer>
<Stack.Navigator initialRouteName="UploadProduct">
<Stack.Screen name="UploadProduct" component={UploadProduct}
options={{title:'UploadProduct'}}/>
<Stack.Screen name="Product" component={Product}
options={{title:'Product'}} />
</Stack.Navigator>
</NavigationContainer>
);
}
}
5) 버튼을 눌렸을 때 어느 페이지로 갈지 정해주는 함수 만들어주기 <UploadProduct.js>
goHomeSreen(){
this.props.navigation.navigate('GetGoods');
}
6) button의 onpress에 함수 적용 <UploadProduct.js>
<Button title="상세보기" onPress={()=>this.goHomeSreen() } />
- 결과 화면
Tab Navigator
1) Tab 을 위한 라이브러리 설치 ( 아이콘 설정을 위해 아이콘도 같이 )
npm install @react-navigation/bottom-tabs react-native-vector-icons
2) 필요한 component를 import 시켜주기
import React, { Component } from "react";
import { View, FlatList, StyleSheet,Text } from 'react-native';
import { NavigationContainer } from '@react-navigation/native';
//Stack 과 Tab
import { createNativeStackNavigator } from '@react-navigation/native-stack';
import { createBottomTabNavigator } from "@react-navigation/bottom-tabs";
//경로를 위한 import
import Product from "./components/Product";
import UploadProduct from "./components/UploadProduct";
import ModalTest from "./components/ModalTest";
3) 경로 지정해주기 - const Tab 추가와 stack으로 되어있던 부분을 Tab으로 수정하면 끝
const Stack = createNativeStackNavigator();//Stack일 경우
const Tab = createBottomTabNavigator();
class App extends Component {
render() {
return (
<>
<NavigationContainer>
<Tab.Navigator initialRouteName="UploadProduct">
<Tab.Screen name="Upload Product" component={UploadProduct} />
<Tab.Screen name="Product" component={Product} />
<Tab.Screen name="모달연습" component={ModalTest} />
</Tab.Navigator>
</NavigationContainer>
</>
);
}
}
- 결과 화면
근데 아이콘이 추가가 안된다.. 서치해서 모든 걸 다 시도해봤는데 안댐... 꼭 성공해서 돌아오리라
(추가) navigation 오류 발생 !!
다른 폴더에서 네비게이션을 다시 설치 해보려고 하니 오류가 마구마구 떴다
해결 방법
npm install @react-navigation/native @react-navigation/native-stack --legacy-peer-deps
뒤에 --legacy-peer-deps가 강제로 설치하게 하는 뜻인 것 같다.
영어를 번역해서 찾은거라 정확하지가 않지만
이렇게 하니까 실행이 되는 것을 확인하고 Tab도 똑같은 방식으로 설치를 해주었다
npm install @react-navigation/native @react-navigation/bottom-tabs --legacy-peer-deps
'React Native' 카테고리의 다른 글
[React-Native] 간단한 Modal 구현 (2) | 2022.10.18 |
---|---|
[React-Native] React-Native Icon 적용 (0) | 2022.10.17 |
[React-Native] FlatList로 Json 스크롤해서 보기 (0) | 2022.10.10 |
[React-Native] Json 파일 가져오기 (0) | 2022.10.04 |
[React-Native] 리액트 네이티브 환경설정 (0) | 2022.09.27 |