使用react-navigation

安装

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 @react-navigation/native

代码

  • 创建router/nav.js
    import React from 'react';
    import { NavigationContainer, NavigationState } from '@react-navigation/native';
    import {
      createStackNavigator,
      TransitionPresets,
    } from '@react-navigation/stack';
    //登录
    import LoginAndRegister from '../pages/users/loginAndRegister';
    // 工具
    import LocalStorageUtils from '../utils/LocalStorageUtils';
    
    const Stack = createStackNavigator();
    
    class Nav extends React.Component {
      constructor(props) {
        super(props);
        // 之前在mobx中的index.js中把所有的信息都存到了userInfo中
        LocalStorageUtils.get('userInfo').then((userInfo) => {
          if (userInfo !== null) {
            // 1.重新获取用户信息
            // 2.存储到mobx中
          } else {
            // 跳转登录
    
            // 使用了mobx
            // this.props.navigate()
          }
        });
      }
    
      render() {
        return (
          <NavigationContainer
            onStateChange={(state: NavigationState) => {
              NavigationHelper.navRouters = state.routes;
            }}
          >
            <Stack.Navigator
              // 一打开页面的时候,先显示哪一个页面
              initialRouteName="Tab"
              // 隐藏顶部的大标题
              headerMode={'none'}
              mode={'card'}
              screenOptions={(navigation) => {
                NavigationHelper.navigation = navigation.navigation;
              }}
            >
              {/*用户*/}
              {/*登录*/}
              <Stack.Screen
                name="LoginAndRegister"
                component={LoginAndRegister}
                options={{
                  ...TransitionPresets.SlideFromRightIOS,
                }}
              />
            </Stack.Navigator>
          </NavigationContainer>
        );
      }
    }
    
    export default Nav;
    
    
  • app.js
    import React, { Component } from 'react';
    import { StatusBar, View } from 'react-native';
    import Nav from './src/router/nav';
    import { Provider } from 'mobx-react';
    import RootStore from './src/mobx/index';
    import './src/utils/fontUtils';
    // socket.io
    
    class App extends Component {
      constructor(props) {
        super(props);
        this.state = {};
      }
    
      render() {
        return (
          <View style={{ flex: 1 }}>
            <StatusBar
              translucent={true}
              backgroundColor={'transparent'}
              barStyle={'dark-content'}
            />
            <Provider RootStore={RootStore}>
              <Nav />
            </Provider>
          </View>
        );
      }
    }
    
    export default App;
    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值