as

Settings
Sign out
Notifications
Alexa
亚马逊应用商店
Ring
AWS
文档
Support
Contact Us
My Cases
新手入门
设计和开发
应用发布
参考
支持

react-navigation

react-navigation

React Navigation是适用于React Native应用的强大导航库,它为管理屏幕过渡、处理路由和实现导航模式(例如底部选项卡、堆栈导航和抽屉导航)提供了一种简单而高效的方式。

React Native没有像网页浏览器那样的内置导航API。React Navigation为您提供此功能,还提供用于在屏幕之间切换的iOS、Android和Vega手势和动画。

这是一个系统部署的库,可供Vega应用使用,无需单独的安装过程。它作为自动链接库进行部署,您的应用在运行时会链接到该库。只能在库和为其构建的Vega版本之间保证兼容性。

当您升级用于构建应用的Vega版本时,最佳实践是同时升级它所依赖的库的版本。

文档

请查看专门的文档页面documentation page(仅提供英文版),了解有关此库、API参考等的信息:

安装

  1. package.json文件中为所需的所有程序包添加依赖项。选择适用于您的React Native版本的选项卡。您并不需要下列所有程序包,因此请在应用程序中包含所需的程序包。


    已复制到剪贴板。

    "dependencies": {
          ...
          "@amazon-devices/react-navigation__bottom-tabs": "~7.0.0",
          "@amazon-devices/react-navigation__core": "~7.0.0",
          "@amazon-devices/react-navigation__devtools": "~7.0.0",
          "@amazon-devices/react-navigation__drawer": "~7.0.0",
          "@amazon-devices/react-navigation__elements": "~7.0.0",
          "@amazon-devices/react-navigation__material-bottom-tabs": "~7.0.0",
          "@amazon-devices/react-navigation__material-top-tabs": "~7.0.0",
          "@amazon-devices/react-navigation__native": "~7.0.0",
          "@amazon-devices/react-navigation__native-stack": "~7.0.0",
          "@amazon-devices/react-navigation__react-native-tab-view": "~7.0.0",
          "@amazon-devices/react-navigation__routers": "~7.0.0",
          "@amazon-devices/react-navigation__stack": "~7.0.0",
          ...
    }
    

    已复制到剪贴板。

    "dependencies": {
          ...
          "@amazon-devices/react-navigation__bottom-tabs": "~8.0.0",
          "@amazon-devices/react-navigation__core": "~8.0.0",
          "@amazon-devices/react-navigation__devtools": "~8.0.0",
          "@amazon-devices/react-navigation__drawer": "~8.0.0",
          "@amazon-devices/react-navigation__elements": "~8.0.0",
          "@amazon-devices/react-navigation__material-top-tabs": "~8.0.0",
          "@amazon-devices/react-navigation__native": "~8.0.0",
          "@amazon-devices/react-navigation__native-stack": "~8.0.0",
          "@amazon-devices/react-navigation__react-native-tab-view": "~8.0.0",
          "@amazon-devices/react-navigation__routers": "~8.0.0",
          "@amazon-devices/react-navigation__stack": "~8.0.0",
          ...
    }
    
  2. 您可以添加@amazon-devices/react-native-screens依赖项来卸载不可见的屏幕。


    "dependencies": {
          ...
          "@amazon-devices/react-native-screens": "~2.0.0"
          ...
    }
    

    "dependencies": {
          ...
          "@amazon-devices/react-native-screens": "~3.0.0"
          ...
    }
    
  3. 使用npm install命令重新安装依赖项。

示例

堆栈示例:

您需要添加以下依赖项:


"dependencies": {
      ...
      "@amazon-devices/react-navigation__stack": "~7.0.0",
      "@amazon-devices/react-navigation__native": "~7.0.0",
      "@amazon-devices/react-native-screens": "~2.0.0",
      ...
}

"dependencies": {
      ...
      "@amazon-devices/react-navigation__stack": "~8.0.0",
      "@amazon-devices/react-navigation__native": "~8.0.0",
      "@amazon-devices/react-native-screens": "~3.0.0",
      ...
}

将以下代码粘贴App.tsx中:

已复制到剪贴板。


import {enableFreeze, enableScreens} from '@amazon-devices/react-native-screens';
import {NavigationContainer} from '@amazon-devices/react-navigation__native';
import {createStackNavigator, StackNavigationOptions} from '@amazon-devices/react-navigation__stack';
import * as React from 'react';
import {StyleSheet, Text, TouchableOpacity, View} from 'react-native';

function generateRandomColors(n: number): string[] {
  const generatedColors: string[] = [];

  for (let i = 0; i < n; i++) {
    const red = Math.floor(Math.random() * 128 + 64);
    const green = Math.floor(Math.random() * 128 + 64);
    const blue = Math.floor(Math.random() * 128 + 64);
    const hex = `#${red.toString(16).padStart(2, '0')}${green
      .toString(16)
      .padStart(2, '0')}${blue.toString(16).padStart(2, '0')}`;
    generatedColors.push(hex);
  }

  return generatedColors;
}

enableScreens();
enableFreeze();

const Stack = createStackNavigator();

const styles = StyleSheet.create({
  screenContainer: {
    flex: 1,
    alignItems: 'center',
    justifyContent: 'center',
  },
  text: {
    fontSize: 30,
  },
  button: {
    width: 200,
    height: 100,
    alignItems: 'center',
    justifyContent: 'center',
  },
  buttonContainer: {
    flexWrap: 'wrap',
  },
});

const numberOfScreens = 4;

const colors = generateRandomColors(numberOfScreens);

const Screen = ({navigation, route}: {navigation: any; route: any}) => {
  const {id} = route.params;
  return (
    <View style={[styles.screenContainer, {backgroundColor: colors[id]}]}>
      <Text style={styles.text}>SCREEN {id}</Text>
      <View style={styles.buttonContainer}>
        {colors.map((color, buttonId) => (
          <TouchableOpacity
            onPress={() => navigation.navigate('Screen' + buttonId)}>
            <View style={[styles.button, {backgroundColor: color}]}>
              <Text>Go to screen #{buttonId}</Text>
            </View>
          </TouchableOpacity>
        ))}
        <TouchableOpacity onPress={() => navigation.goBack()}>
          <View style={[styles.button, {backgroundColor: 'white'}]}>
            <Text>{'<- 返回'}</Text>
          </View>
        </TouchableOpacity>
      </View>
    </View>
  );
};

const screenOptions: StackNavigationOptions = { animation: 'default' };
export const App = () => (
  <NavigationContainer>
    <Stack.Navigator screenOptions={screenOptions} detachInactiveScreens>
      {colors.map((_, id) => (
        <Stack.Screen
          key={id}
          name={'Screen' + id}
          component={Screen}
          initialParams={{id}}
          options={screenOptions}
        />
      ))}
    </Stack.Navigator>
  </NavigationContainer>
);

原生堆栈示例:

您需要添加以下依赖项:


"dependencies": {
      ...
      "@amazon-devices/react-navigation__native-stack": "~7.0.0",
      "@amazon-devices/react-navigation__native": "~7.0.0",
      "@amazon-devices/react-native-screens": "~2.0.0",
      "@amazon-devices/react-native-safe-area-context": "~2.0.0",
      ...
}

"dependencies": {
      ...
      "@amazon-devices/react-navigation__native-stack": "~8.0.0",
      "@amazon-devices/react-navigation__native": "~8.0.0",
      "@amazon-devices/react-native-screens": "~3.0.0",
      "@amazon-devices/react-native-safe-area-context": "~8.0.9000000000",
      ...
}

将以下代码粘贴App.tsx中:

已复制到剪贴板。


import {enableFreeze, enableScreens} from '@amazon-devices/react-native-screens';
import {createNativeStackNavigator} from '@amazon-devices/react-navigation__native-stack';
import {NavigationContainer} from '@amazon-devices/react-navigation__native';
import * as React from 'react';
import {StyleSheet, Text, TouchableOpacity, View} from 'react-native';

function generateRandomColors(n: number): string[] {
  const generatedColors: string[] = [];

  for (let i = 0; i < n; i++) {
    const red = Math.floor(Math.random() * 128 + 64);
    const green = Math.floor(Math.random() * 128 + 64);
    const blue = Math.floor(Math.random() * 128 + 64);
    const hex = `#${red.toString(16).padStart(2, '0')}${green
      .toString(16)
      .padStart(2, '0')}${blue.toString(16).padStart(2, '0')}`;
    generatedColors.push(hex);
  }

  return generatedColors;
}

enableScreens();
enableFreeze();

const Stack = createNativeStackNavigator();

const styles = StyleSheet.create({
  screenContainer: {
    flex: 1,
    alignItems: 'center',
    justifyContent: 'center',
  },
  text: {
    fontSize: 30,
  },
  button: {
    width: 200,
    height: 100,
    alignItems: 'center',
    justifyContent: 'center',
  },
  buttonContainer: {
    flexWrap: 'wrap',
  },
});

const numberOfScreens = 4;

const colors = generateRandomColors(numberOfScreens);

const Screen = ({navigation, route}: {navigation: any; route: any}) => {
  const {depth, id} = route.params;
  return (
    <View
      style={[
        styles.screenContainer,
        {backgroundColor: colors[id], margin: 20 * depth},
      ]}>
      <Text style={styles.text}>SCREEN {id}</Text>
      <View style={styles.buttonContainer}>
        {colors.map((color, buttonId) => (
          <TouchableOpacity
            onPress={() =>
              navigation.navigate('Screen' + buttonId, {depth: depth + 1})
            }>
            <View style={[styles.button, {backgroundColor: color}]}>
              <Text>Go to screen #{buttonId}</Text>
            </View>
          </TouchableOpacity>
        ))}
        <TouchableOpacity onPress={() => navigation.goBack()}>
          <View style={[styles.button, {backgroundColor: 'white'}]}>
            <Text>{'<- 返回'}</Text>
          </View>
        </TouchableOpacity>
      </View>
    </View>
  );
};

const screenOptions = {
  headerShown: false,
};

export const App = () => (
  <NavigationContainer>
    <Stack.Navigator screenOptions={screenOptions}>
      {colors.map((_, id) => (
        <Stack.Screen
          key={id}
          name={'Screen' + id}
          component={Screen}
          initialParams={{id, depth: 0}}
          options={screenOptions}
        />
      ))}
    </Stack.Navigator>
  </NavigationContainer>
);

底部选项卡示例:

您需要添加以下依赖项:


"dependencies": {
      ...
      "@amazon-devices/react-navigation__bottom-tabs": "~7.0.0",
      "@amazon-devices/react-navigation__native": "~7.0.0",
      "@amazon-devices/react-native-screens": "~2.0.0",
      ...
}

"dependencies": {
      ...
      "@amazon-devices/react-navigation__bottom-tabs": "~8.0.0",
      "@amazon-devices/react-navigation__native": "~8.0.0",
      "@amazon-devices/react-native-screens": "~3.0.0",
      ...
}

将以下代码粘贴App.tsx中:

已复制到剪贴板。


import { enableScreens } from '@amazon-devices/react-native-screens';
import { createBottomTabNavigator } from '@amazon-devices/react-navigation__bottom-tabs';
import { NavigationContainer } from '@amazon-devices/react-navigation__native';
import * as React from 'react';
import { Text, TouchableOpacity,View } from 'react-native';

enableScreens();

function HomeScreen() {
  return (
    <View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
      <Text>Home!</Text>
    </View>
  );
}

function SettingsScreen() {
  const [text, setText] = React.useState("Press me to check if touches work");
  return (
    <View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
      <TouchableOpacity onPress={() => setText("Touching works!")}>
        <View style={;{ width: 200, height: 200, backgroundColor: "skyblue" }}>
          <Text>{text}</Text>
        </View>
      </TouchableOpacity>
      <Text>设置!</Text>
    </View>
  );
}

const Tab = createBottomTabNavigator();

export const App = () => {
  return (
    <NavigationContainer>
      <Tab.Navigator detachInactiveScreens>
        <Tab.Screen name="主页" component={HomeScreen} />
        <Tab.Screen name="设置" component={SettingsScreen} />
      </Tab.Navigator>
    </NavigationContainer>
  );
}

Material顶部选项卡示例:

您需要添加以下依赖项:


"dependencies": {
      ...
      "@amazon-devices/react-navigation__material-top-tabs": "~7.0.0",
      "@amazon-devices/react-navigation__native": "~7.0.0",
      "@amazon-devices/react-native-screens": "~2.0.0"
      ...
}

"dependencies": {
      ...
      "@amazon-devices/react-navigation__material-top-tabs": "~8.0.0",
      "@amazon-devices/react-navigation__native": "~8.0.0",
      "@amazon-devices/react-native-screens": "~3.0.0"
      ...
}

将以下代码粘贴App.tsx中:

已复制到剪贴板。


import { enableFreeze, enableScreens } from '@amazon-devices/react-native-screens';
import { createMaterialTopTabNavigator } from '@amazon-devices/react-navigation__material-top-tabs';
import { NavigationContainer } from '@amazon-devices/react-navigation__native';
import * as React from 'react';
import { Text, TouchableOpacity,View } from 'react-native';

enableScreens();
enableFreeze();

function HomeScreen() {
  return (
    <View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
      <Text>Home!</Text>
    </View>
  );
}

function SettingsScreen() {
  const [text, setText] = React.useState("Press me to check if touches work");
  return (
    <View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
      <TouchableOpacity onPress={() => setText("Touching works!")}>
        <View style={{ width: 200, height: 200, backgroundColor: "skyblue" }}>
          <Text>{text}</Text>
        </View>
      </TouchableOpacity>
      <Text>设置!</Text>
    </View>
  );
}

const Tab =
createMaterialTopTabNavigator();

export const App = () => {
  return (
    <NavigationContainer>
      <Tab.Navigator>
        <Tab.Screen name="主页" component={HomeScreen} />
        <Tab.Screen name="设置" component={SettingsScreen} />
      </Tab.Navigator>
    </NavigationContainer>
  );
}

抽屉示例:

您需要添加以下依赖项:


"dependencies": {
      ...
      "@amazon-devices/react-native-gesture-handler": "~2.0.0",
      "@amazon-devices/react-native-reanimated": "~2.0.0",
      "@amazon-devices/react-navigation__drawer": "~7.0.0",
      "@amazon-devices/react-navigation__native": "~7.0.0",
      ...
}

"dependencies": {
      ...
      "@amazon-devices/react-native-gesture-handler": "~4.0.0",
      "@amazon-devices/react-native-reanimated": "~4.0.0",
      "@amazon-devices/react-navigation__drawer": "~8.0.0",
      "@amazon-devices/react-navigation__native": "~8.0.0",
      ...
}

将reanimated插件添加到babel.config.js


module.exports = {
  presets: [...],
  plugins: ['@amazon-devices/react-native-reanimated/plugin'] // 在此处添加
};

在运行应用之前重置metro缓存:

npm start -- --reset-cache

将以下代码粘贴App.tsx中:

已复制到剪贴板。


import {GestureHandlerRootView} from '@amazon-devices/react-native-gesture-handler';
import {createDrawerNavigator} from '@amazon-devices/react-navigation__drawer';
import {NavigationContainer} from '@amazon-devices/react-navigation__native';
import React, {useState} from 'react';
import {Text, TouchableOpacity, View} from 'react-native';

function HomeScreen() {
  return (
    <View style={{flex: 1, justifyContent: 'center', alignItems: 'center'}}>
      <Text>Home!</Text>
    </View>
  );
}

function SettingsScreen() {
  const [text, setText] = useState('点按此处,测试触摸是否正常');
  return (
    <View style={{flex: 1, justifyContent: 'center', alignItems: 'center'}}>
      <TouchableOpacity onPress={() => setText('触摸功能正常!')}>
        <View style={{width: 200, height: 200, backgroundColor: 'skyblue'}}>
          <Text>{text}</Text>
        </View>
      </TouchableOpacity>
      <Text>设置!</Text>
    </View>
  );
}

const Drawer = createDrawerNavigator();

export const App = () => (
  <GestureHandlerRootView style={{flex: 1, backgroundColor: 'white'}}>
    <NavigationContainer>
      <Drawer.Navigator initialRouteName="Home" detachInactiveScreens={false}>
        <Drawer.Screen name="Home" component={HomeScreen} />
        <Drawer.Screen name="Settings" component={SettingsScreen} />
      </Drawer.Navigator>
    </NavigationContainer>
  </GestureHandlerRootView>
);

已知问题

Vega上不支持API

Vega上的React-Navigation库增加了对React-Navigation v6中所有可用API的支持。

导航类型 描述
堆栈导航器 堆栈导航器是一种让您的应用在不同屏幕之间切换的方式,其中每个新屏幕都位于堆栈的顶部。
原生堆栈导航器 原生堆栈导航器支持应用在不同屏幕之间切换,其中每个新屏幕都会通过原生API放置在堆栈顶部。
抽屉导航器 抽屉导航器在屏幕侧面渲染一个导航抽屉,可以通过手势打开和关闭该抽屉。
底部选项卡导航器 屏幕底部的简单选项卡栏,可让您在不同的路线之间切换。
Material底部选项卡导航器 屏幕底部以material-design为主题的选项卡栏,可让您通过动画在不同的路线之间切换。这封装了react-native-paper中的BottomNavigation组件。
Material顶部选项卡导航器 屏幕顶部以material-design为主题的选项卡栏,可让您通过点击选项卡或水平滑动来在不同的路线之间切换。这封装了react-native-tab-view程序包。

组件

组件名称 描述
NavigationContainer 它负责管理应用状态并将顶级导航器链接到应用环境。
ServerContainer 提供实用工具,使您的应用在服务器上以正确的导航状态渲染。
Group 用于对导航器内的多个屏幕进行分组的组件。
Screen 屏幕组件用于配置导航器内屏幕的各个方面。屏幕组件作为createXNavigator函数的一个属性返回。
Link 用于渲染可在按下时导航到屏幕的组件。在网页上使用时,它会渲染<a>标签。在其他平台上,它使用Text组件。

Vega上不支持

导航库在API支持方面有一些例外。

  • Vega平台尚不支持flipper-plugin-react-navigation

支持的版本

下表列出了Vega SDK支持的React Navigation程序包。

NPM程序包版本 Vega SDK版本 Vega OS版本 React Native版本
^7.0.x 0.23及更早版本 操作系统1.1 (1401010009820)及更早版本 0.72
^8.0.x 0.24 操作系统1.2 (2101020054720) 0.83

React Native 0.83子程序包

程序包名称 程序包版本 @amazon-devices/react-native-kepler版本
@amazon-devices/react-navigation__bottom-tabs 8.0.1+7.8.7 4.0.x+rn0.83.0
@amazon-devices/react-navigation__core 8.0.1+7.13.3 4.0.x+rn0.83.0
@amazon-devices/react-navigation__devtools 8.0.1+7.0.42 4.0.x+rn0.83.0
@amazon-devices/react-navigation__drawer 8.0.1+7.7.5 4.0.x+rn0.83.0
@amazon-devices/react-navigation__elements 8.0.1+2.8.4 4.0.x+rn0.83.0
@amazon-devices/react-navigation__material-top-tabs 8.0.1+7.4.5 4.0.x+rn0.83.0
@amazon-devices/react-navigation__native 8.0.1+7.1.22 4.0.x+rn0.83.0
@amazon-devices/react-navigation__native-stack 8.0.1+7.8.1 4.0.x+rn0.83.0
@amazon-devices/react-navigation__react-native-tab-view 8.0.1+4.2.0 4.0.x+rn0.83.0
@amazon-devices/react-navigation__routers 8.0.1+7.5.2 4.0.x+rn0.83.0
@amazon-devices/react-navigation__stack 8.0.1+7.6.8 4.0.x+rn0.83.0
@amazon-devices/keplerscript-flipper-plugin-react-navigation 不支持 不支持

React Native 0.72子程序包

程序包名称 程序包版本 @amazon-devices/react-native-kepler版本
@amazon-devices/react-navigation__bottom-tabs 7.0.0 2.0.0+rn0.72.0
@amazon-devices/react-navigation__core 7.0.0 2.0.0+rn0.72.0
@amazon-devices/react-navigation__devtools 7.0.0 2.0.0+rn0.72.0
@amazon-devices/react-navigation__drawer 7.0.0 2.0.0+rn0.72.0
@amazon-devices/react-navigation__elements 7.0.0 2.0.0+rn0.72.0
@amazon-devices/react-navigation__material-bottom-tabs 7.0.0 2.0.0+rn0.72.0
@amazon-devices/react-navigation__material-top-tabs 7.0.0 2.0.0+rn0.72.0
@amazon-devices/react-navigation__native 7.0.0 2.0.0+rn0.72.0
@amazon-devices/react-navigation__native-stack 7.0.0 2.0.0+rn0.72.0
@amazon-devices/react-navigation__react-native-tab-view 7.0.0 2.0.0+rn0.72.0
@amazon-devices/react-navigation__routers 7.0.0 2.0.0+rn0.72.0
@amazon-devices/react-navigation__stack 7.0.0 2.0.0+rn0.72.0
@amazon-devices/keplerscript-flipper-plugin-react-navigation 不支持 不支持

其他资源

有关其他库的信息,请参阅支持的第三方库和服务


Last updated: 2026年7月28日