模板版本:v0.2.2
[!TIP] Github 地址
进入到工程目录并输入以下命令:
npm install @react-navigation/[email protected]
yarn add @react-navigation/[email protected]
下面的代码展示了这个库的基本使用场景:
[!WARNING] 使用时 import 的库名不变。
import * as React from 'react';
import { Text, View } from 'react-native';
import { NavigationContainer } from '@react-navigation/native';
import { createBottomTabNavigator } from '@react-navigation/bottom-tabs';
const Tab = createBottomTabNavigator();
function HomeScreen({ navigation }) {
return (
<View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
<Text>Home screen</Text>
</View>
);
}
function DetailsScreen({ navigation }) {
return (
<View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
<Text>Details!</Text>
</View>
);
}
export default function App() {
return (
<NavigationContainer>
<Tab.Navigator>
<Tab.Screen name="Home" component={HomeScreen} />
<Tab.Screen name="Details" component={DetailsScreen} />
</Tab.Navigator>
</NavigationContainer>
);
}
export {App}
本文档内容基于以下版本验证通过:
- RNOH: 0.72.11; SDK: OpenHarmony(api11) 4.1.0.53; IDE: DevEco Studio 4.1.3.412; ROM: 2.0.0.52;
- RNOH: 0.72.13; SDK: HarmonyOS NEXT Developer Preview1; IDE: DevEco Studio 4.1.3.500; ROM: 2.0.0.58;
- RNOH: 0.72.20-CAPI; SDK: HarmonyOS NEXT Developer Beta1 B.0.18; IDE: DevEco Studio 5.0.3.200; ROM: 3.0.0.18;
- RNOH: 0.72.27; SDK: HarmonyOS-Next-DB1 5.0.0.25; IDE: DevEco Studio 5.0.3.400SP7; ROM: 3.0.0.25;
[!TIP] "Platform"列表示该属性在原三方库上支持的平台。
[!TIP] "HarmonyOS Support"列为 yes 表示 HarmonyOS 平台支持该属性;no 则表示不支持;partially 表示部分支持。使用方法跨平台一致,效果对标 iOS 或 Android 的效果。
NavigationContainer:
Name | Description | Type | Required | Platform | HarmonyOS Support |
---|---|---|---|---|---|
useNavigationContainerRef | A ref link to navigation | function | no | all | yes |
onStateChange | Function that gets called every time navigation state changes. It receives the new navigation state as the argument. | function | no | all | yes |
onReady | Function which is called after the navigation container and all its children finish mounting for the first time. | function | no | all | yes |
Hooks
Name | Description | Type | Required | Platform | HarmonyOS Support |
---|---|---|---|---|---|
useScrollToTop | The expected native behavior of scrollable components is to respond to events from navigation that will scroll to top when tapping on the active tab as you would expect from native tab bars. | function | no | all | yes |
示例代码依赖以下三方库,请查看对应文档:
- @react-navigation/bottom-tabs
- @react-native-oh-tpl/react-native-gesture-handler
- @react-native-oh-library/react-native-safe-area-context
本项目基于 The MIT License (MIT) ,请自由地享受和参与开源。