React Native 集成react-navigation(导航组件)及代码示例

本文详细介绍如何在ReactNative项目中集成react-navigation组件,包括创建项目、安装必要库、配置Android环境及修改入口文件的步骤,并附带代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

React Native 集成react-navigation(导航组件)及代码示例

1、创建测试项目navTest

#使用命令
react-native init navTest

#使用webstrom创建

在这里插入图片描述

2、安装react-navigation

#安装
yarn add react-navigation
# or 
npm install --save react-navigation

3、安装react-native-gesture-handler

1):安装

#安装
yarn add react-native-gesture-handler
# or 
npm install --save react-native-gesture-handler

#链接所有本机依赖
react-native link react-native-gesture-handler

2):Android配置需要修MainActivity.java文件

#修改/android/app/src/main/java/com/navtest/MainActivity.java文件

#需要导入的依赖
+ import com.facebook.react.ReactActivityDelegate;
+ import com.facebook.react.ReactRootView;
+ import com.swmansion.gesturehandler.react.RNGestureHandlerEnabledRootView;

#需要重写的方法
+  @Override
+  protected ReactActivityDelegate createReactActivityDelegate() {
+    return new ReactActivityDelegate(this, getMainComponentName()) {
+      @Override
+      protected ReactRootView createRootView() {
+       return new RNGestureHandlerEnabledRootView(MainActivity.this);
+      }
+    };
+  }

4、配置完成、测试。

1):修改App.js入口文件

import React from "react";
import {Text, View} from "react-native";
import {createAppContainer, createStackNavigator} from "react-navigation";

class HelloScreen extends React.Component {
    render() {
        return (
            <View style={{flex: 1, alignItems: "center", justifyContent: "center"}}>
                <Text>Hello react-navigation</Text>
            </View>
        );
    }
}

const AppNavigator = createStackNavigator({
    HelloScreen: {
        screen: HelloScreen,
        navigationOptions: {
            title: 'Hello'
        }
    }
});

export default createAppContainer(AppNavigator);

2):运行

#运行到安卓模拟器
react-native run-android

3):效果图

在这里插入图片描述

评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值