반응형
    
    
    
  앱에서 라우팅 및 네비게이션을 제공하기 위해서 React Navigation 적용방법을 정리한다.
> 작성일 : 2025-01-27
> OS : macOS Sequoia 15.2 (Apple M4)
> NodeJS : v22.13.1
> NPM : v10.9.2
> ReactNative : v0.77
<1> React Navigation 설치
생성된 프로젝트에서 아래 명령을 수행
npm install @react-navigation/native
npm install react-native-screens react-native-safe-area-context
<2> Android MainActivity 설정
프로젝트 android/app/src/main/java/<packagename>/MainActivity.kt 파일을 아래와 같이 수정
package com.mungpl_app
import com.facebook.react.ReactActivity
import com.facebook.react.ReactActivityDelegate
import com.facebook.react.defaults.DefaultNewArchitectureEntryPoint.fabricEnabled
import com.facebook.react.defaults.DefaultReactActivityDelegate
/**
 * Set for React Navigation
 */
import android.os.Bundle
class MainActivity : ReactActivity() {
  /**
   * Returns the name of the main component registered from JavaScript. This is used to schedule
   * rendering of the component.
   */
  override fun getMainComponentName(): String = "mungpl_app"
  /**
   * Returns the instance of the [ReactActivityDelegate]. We use [DefaultReactActivityDelegate]
   * which allows you to enable New Architecture with a single boolean flags [fabricEnabled]
   */
  override fun createReactActivityDelegate(): ReactActivityDelegate =
      DefaultReactActivityDelegate(this, mainComponentName, fabricEnabled)
  /**
   * Set for React Navigation
   */
  override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(null)
  }
}
<3> NavigationContainer
앱이 NavigationContainer로 감싸진 구성이어야 한다.
import * as React from 'react';
import { NavigationContainer } from '@react-navigation/native';
export default function App() {
  return (
    <NavigationContainer>React Navigation Test</NavigationContainer>
  );
}
<4> Stack & Native Stack & Bottom Tabs
네비게이션 구성에는 기본적으로 Stack Navigator, Native Stack Navigator, Bottom Tab Navigator 을 기반으로 구현된다.
- Stack Navigator - Link
npm install @react-navigation/stack
npm install react-native-gesture-handler- Native Stack Navigator - Link
npm install @react-navigation/native-stack- Bottom Tabs - Link
npm install @react-navigation/bottom-tabsReact Navigation GitHub에 다양한 예제를 제공한다. 개발하고자 하는 앱의 형태와 적합한 구성을 찾아 참고한다.

- React Navigation Example - Link
반응형
    
    
    
  'Setting' 카테고리의 다른 글
| [Setting | ReactNative] 개발환경 구축하기 (4) - Icon 적용 (0) | 2025.01.28 | 
|---|---|
| [Setting | MacOS] Java (Jdk) Install (0) | 2025.01.27 | 
| [Setting | ReactNative] 개발환경 구축하기 (2) - Tailwind 적용 (0) | 2025.01.26 | 
| [Setting | ReactNative] 개발환경 구축하기 (1) - 기본앱 설치 및 실행 (0) | 2025.01.25 | 
| [Setting | MacOS] Xcode Install (With. Command Line Tools / Simulator) (0) | 2025.01.23 |