安装
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);
LocalStorageUtils.get('userInfo').then((userInfo) => {
if (userInfo !== null) {
} else {
}
});
}
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';
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;