在 AuthLoadingScreen 中检查用户令牌(在您的情况下为欢迎屏幕)。
并根据用户令牌分散到SignUp 屏幕或Home。
例如...
- 将
WelcomeScreen(AuthLoading)、Auth(SignUp, SignIn) 和Home( and others screen) 包装到createStackNavigator。
App.js
import { createSwitchNavigator, createStackNavigator } from 'react-navigation';
// Implementation of HomeScreen, OtherScreen, SignInScreen, AuthLoadingScreen
// goes here.
const AppStack = createStackNavigator({ Home: HomeScreen, Other: OtherScreen });
const AuthStack = createStackNavigator({ SignIn: SignInScreen });
export default createSwitchNavigator(
{
AuthLoading: AuthLoadingScreen,
App: AppStack,
Auth: AuthStack,
},
{
initialRouteName: 'AuthLoading',
}
);
- 在
constructor of AuthLoadingScreen Class 中写入检查用户令牌。
AuthLoadingScreen.js
import React from 'react';
import {
ActivityIndicator,
AsyncStorage,
StatusBar,
StyleSheet,
View,
} from 'react-native';
class AuthLoadingScreen extends React.Component {
constructor(props) {
super(props);
this._bootstrapAsync();
}
// Fetch the token from storage then navigate to our appropriate place
_bootstrapAsync = async () => {
const userToken = await AsyncStorage.getItem('userToken');
// This will switch to the App screen or Auth screen and this loading
// screen will be unmounted and thrown away.
this.props.navigation.navigate(userToken ? 'App' : 'Auth');
};
// Render any loading content that you like here
render() {
return (
<View style={styles.container}>
<ActivityIndicator />
<StatusBar barStyle="default" />
</View>
);
}
}
重要的是如何将导航中的屏幕包装为堆栈、抽屉和点击。
您可以通过多种方式控制堆栈
- 导航:转到另一个屏幕
this.props.navigation.navigate('yourscreen')
- goBack:关闭活动屏幕并返回
this.props.navigation.goBack()
特别是,当屏幕包含在堆栈中时,会有更多的控制。
- popToTop: 到栈顶
this.props.navigation.popToTop()
- 推:你会知道该怎么做。
- 流行:
- replace:用新的路由替换当前路由
this.props.navigation.replace(yourscreen')
参考:https://reactnavigation.org/docs/en/auth-flow.html