官方文档

https://reactnavigation.org/

image.png

  1. 安装

react-navigation现在的不同路由器,需要分别安装。

  1. yarn add react-native-reanimated react-native-gesture-handler react-native-screens react-native-safe-area-context @react-native-community/masked-view @react-navigation/stack

官网的依赖(实操可行)
基础导航 native-stack(“栈”路由)

  1. @react-navigation/native
  2. react-native-screens
  3. react-native-safe-area-context
  4. @react-navigation/native-stack
  1. yarn add @react-navigation/native react-native-screens react-native-safe-area-context @react-navigation/native-stack

react-native-screens
安卓:修改\android\app\src\main\java\com\\MainActivity.java文件代码

  1. ...
  2. // 导入Bundle类,该类的格式为key-value,用于实现数据的传递
  3. import android.os.Bundle;
  4. public class MainActivity extends ReactActivity {
  5. // 覆写生命周期函数,在Activity被创建时调用
  6. // 而Bundle类型的savedInstanceState可以保存状态数据
  7. @Override
  8. protected void onCreate(Bundle savedInstanceState) {
  9. super.onCreate(null);
  10. }
  11. /**
  12. * Returns the name of the main component registered from JavaScript. This is used to schedule
  13. * rendering of the component.
  14. */
  15. @Override
  16. protected String getMainComponentName() {
  17. return "demo1";
  18. }
  19. }

路由组件必须只能有一个,而且必须是根组件

  1. import AppNavigator from '~/navigation';
  2. // ...
  3. render() {
  4. // const {isLoading} = this.state;
  5. return (
  6. <AppNavigator />
  7. // <SafeAreaView>
  8. // <View style={styles.logoWare}>
  9. // <StatusBar backgroundColor="transparent" translucent={true} />
  10. // <Image style={styles.logo} source={require('~/assets/img/cat.jpg')} />
  11. // <Login />
  12. // <Loading isLoading={isLoading} />
  13. // <Svg icon="male" size={px2dp(45)} style={styles.writeBtn} />
  14. // <Svg icon="female" size={px2dp(45)} style={styles.writeBtn} />
  15. // <LinearBtn
  16. // style={{width: px2dp(300), height: px2dp(50)}}
  17. // textMsg="获取验证码"
  18. // />
  19. // </View>
  20. // </SafeAreaView>
  21. );
  22. }

Android导航标题居中问题

headerTitleAlign: ‘center’, // 标题居中 android/ios

  1. <NavigationContainer>
  2. <Stack.Navigator headerMode="none" initialRouteName='Home' screenOptions={{
  3. headerShadowVisible: false, // android 导航去阴影
  4. headerTitleAlign: 'center', // 标题居中 android/ios
  5. // 设置导航栏字体样式
  6. headerTitleStyle: {
  7. fontSize: 17,
  8. color: '#fff',
  9. fontFamily: 'PingFangSC-Semibold',
  10. fontWeight: 'bold',
  11. },
  12. headerStyle: {
  13. backgroundColor: '#f4511e',
  14. },
  15. // 标题右侧功能组件
  16. headerRight: () => (
  17. <Button
  18. onPress={() => alert('This is a button!')}
  19. title="Info"
  20. color="red"
  21. />
  22. ),
  23. }}>
  24. <Stack.Screen name="Home" component={HomeScreen} options={{
  25. title: '首页',
  26. }} />
  27. <Stack.Screen name="Details" component={DetailScreen} />
  28. </Stack.Navigator>
  29. </NavigationContainer>

导航调用堆栈

@react-navigation/native-stack

  1. import * as React from 'react';
  2. import { Button, Text, View } from 'react-native';
  3. import { NavigationContainer } from '@react-navigation/native';
  4. import { createNativeStackNavigator } from '@react-navigation/native-stack';
  5. import { createBottomTabNavigator } from '@react-navigation/bottom-tabs';
  6. function DetailsScreen() {
  7. return (
  8. <View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
  9. <Text>Details!</Text>
  10. </View>
  11. );
  12. }
  13. function HomeScreen({ navigation }) {
  14. return (
  15. <View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
  16. <Text>Home screen</Text>
  17. <Button
  18. title="Go to Details"
  19. onPress={() => navigation.navigate('Details')}
  20. />
  21. </View>
  22. );
  23. }
  24. function SettingsScreen({ navigation }) {
  25. return (
  26. <View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
  27. <Text>Settings screen</Text>
  28. <Button
  29. title="Go to Details"
  30. onPress={() => navigation.navigate('Details')}
  31. />
  32. </View>
  33. );
  34. }
  35. const HomeStack = createNativeStackNavigator();
  36. function HomeStackScreen() {
  37. return (
  38. <HomeStack.Navigator>
  39. <HomeStack.Screen name="Home" component={HomeScreen} />
  40. <HomeStack.Screen name="Details" component={DetailsScreen} />
  41. </HomeStack.Navigator>
  42. );
  43. }
  44. const SettingsStack = createNativeStackNavigator();
  45. function SettingsStackScreen() {
  46. return (
  47. <SettingsStack.Navigator>
  48. <SettingsStack.Screen name="Settings" component={SettingsScreen} />
  49. <SettingsStack.Screen name="Details" component={DetailsScreen} />
  50. </SettingsStack.Navigator>
  51. );
  52. }
  53. const Tab = createBottomTabNavigator();
  54. export default function App() {
  55. return (
  56. <NavigationContainer>
  57. <Tab.Navigator>
  58. <Tab.Screen name="Home" component={HomeStackScreen} />
  59. <Tab.Screen name="Settings" component={SettingsStackScreen} />
  60. </Tab.Navigator>
  61. </NavigationContainer>
  62. );
  63. }