最新消息:看到那些跳动的图片、文字了吗?点击点击 O(∩_∩)O~~

react navigation 6.x 一些小变更

若思若想 onlyling 1418浏览

依赖变更

React Native 最低支持 0.63.0,同时 Expo 的 SDK 最低是 41

  • react-native-safe-area-context >= 3.0.0
  • react-native-screens >= 2.15.0
  • react-native-tab-view >= 3.0.0
  • react-native >= 0.63.0
  • expo – 40+ (if you use Expo)

第三方依赖也减少了一些,在 5.x 版本需要额外安装 react-native-reanimated react-native-gesture-handler react-native-screens react-native-safe-area-context @react-native-community/masked-view这几个第三方库,6.x 目前已经只需要 react-native-screens react-native-safe-area-context,经过实际体验,其实还是需要react-native-gesture-handler

@react-navigation/stack 依赖 react-native-gesture-handler

@react-navigation/drawer 依赖 react-native-reanimated

Android

根据文档提示,Android 需要在 android/app/src/main/java/<your package name>/MainActivity.java 这个文件夹添加一些代码。

package com.onlyling;

import com.facebook.react.ReactActivity;
import android.os.Bundle; // add by react navigation

public class MainActivity extends ReactActivity {

  /**
   * Returns the name of the main component registered from JavaScript. This is used to schedule
   * rendering of the component.
   */
  @Override
  protected String getMainComponentName() {
    return "onlyling";
  }

  // add by react navigation
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(null);
  }
}

其他

@react-navigation/material-top-tabs 所依赖的 react-native-tab-view 已经更新依赖库,原来的它依赖的 @react-native-community/masked-view 已经更名为 react-native-pager-view,如果项目里面有这个依赖,不建议升级。以前遇见一个问题,在 Android 端能运行,iOS 打包出问题。

headerMode="none" 已经被移除,使用 headerShown: false,它又放置到了 Screenoptions 选项里面,原来我们可以在根 Stack.Navigator 统一配置的隐藏头部,如果遇见 bottom-tabs 需要单独配置。

在嵌套路由中,例如 Home 路由是一个 bottom-tabs 路由,里面它还有一个路由名叫做 Home,现在会在控制台提示。

TypeScript 的支持也更完善了,例如根路由实例可以添加泛型。

import { NavigationContainerRef } from '@react-navigation/native';
// ...
const navigationRef = React.createRef<NavigationContainerRef<RootStackParamList>>();

对默认 useNavigation 也可以添加一些默认提示。

declare global {
  // eslint-disable-next-line @typescript-eslint/no-namespace
  namespace ReactNavigation {
    interface RootParamList extends RootStackParamList {}
  }
}

更多内容参考 https://reactnavigation.org/docs/upgrading-from-5.x

转载请注明:OnlyLing - Web 前端开发者 » react navigation 6.x 一些小变更