react-native

导航@react-navigation 版本5.x

2020-12-08  本文已影响0人  _若无
初始化安装:
yarn add react-native-reanimated react-native-gesture-handler react-native-screens react-native-safe-area-context @react-native-community/masked-view
yarn add @react-navigation/native @react-navigation/stack
配置
import 'react-native-gesture-handler';
package com.swmansion.gesturehandler.react.example;
import com.facebook.react.ReactActivity;
+ import com.facebook.react.ReactActivityDelegate;
+ import com.facebook.react.ReactRootView;
+ import com.swmansion.gesturehandler.react.RNGestureHandlerEnabledRootView;
public class MainActivity extends ReactActivity {

  @Override
  protected String getMainComponentName() {
    return "Example";
  }
+  @Override
+  protected ReactActivityDelegate createReactActivityDelegate() {
+    return new ReactActivityDelegate(this, getMainComponentName()) {
+      @Override
+      protected ReactRootView createRootView() {
+       return new RNGestureHandlerEnabledRootView(MainActivity.this);
+      }
+    };
+  }
}
const navigation = useNavigation();
  1. 在根容器中添加一个ref
import { navigate, navRef } from '../utils/navService';

<NavigationContainer ref={navRef}>
    <Stack.Navigator>
      <Stack.Screen name="Home" component={HomeScreen} />
      <Stack.Screen name="Detail" component={DetailScreen} />
    </Stack.Navigator>
</NavigationContainer>
/**
 * created by yue on 2020.12.08
 * 封装导航
 * 
 * 类型检查
 *  ? 表示可选
 */
import * as React from 'react';

export const navRef = React.createRef();

// 需要跳转的地方引入该FN即可
export function navigate(routeName, params) {
  return navRef.current?.navigate(routeName, params);
}

export function goBack() {
  return navRef.current?.goBack();
}

// todos 添加并导出更多想要的Nav操作 
上一篇下一篇

猜你喜欢

热点阅读