我想要做的是在我的应用程序的最顶部包装一个名为 MainNavigator 的切换导航器,它重定向到一个 LoadingScreen,它确定用户是否已记录并导航到 AuthNavigator 或 AppNavigator。AuthNavigator 自己是一个 stackNavigator,他显示屏幕“LoginScreen”或“SignUpScreen”。AppNavigator 是具有多个屏幕(配置文件、主页...)的 DrawerNavigator。
我遇到的问题是返回方法似乎有效,因为我可以从“LoginScreen”组件看到控制台中的日志,但在屏幕上,唯一的返回是白屏。当我直接调用我的“LoginScreen”时使用 LoadingScreen 组件中的组件:
_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. switched to develop the app
this.props.navigation.navigate(userToken ? 'App' : 'LoginScreen');
};
我在我的设备上呈现了 LoginScreen 组件。
我知道问题来自我的 AuthNavigator 和 AppNavigator,因为我可以从 AuthLoading 或 MainNavigator 返回屏幕组件这里有一些代码:
* Main Navigator: entry point of the app
* Redirect to AuthLoadingScreen by default,
* if user is signed (AsyncStorage) or successly signIn
* redirect to AppNavigator else if user isn't SignIn
* go to AuthNavigator
import { createAppContainer, createSwitchNavigator } from 'react-
navigation';
import AuthNavigator from './auth/AuthNavigator'
import AppNavigator from './app/AppNavigator'
import AuthLoadingScreen from '../screens/auth/AuthLoadingScreen'
export default createAppContainer(createSwitchNavigator(
{
// You could add another route here for authentication.
// Read more at https://reactnavigation.org/docs/en/auth-flow.html
AuthLoading: AuthLoadingScreen,
App: AppNavigator,
Auth: AuthNavigator,
},
{
initialRouteName: 'AuthLoading',
}
));
验证加载:
import React from 'react';
import {
ActivityIndicator,
AsyncStorage,
StatusBar,
StyleSheet,
View,
Text
} from 'react-native';
export default 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. switched to develop the app
this.props.navigation.navigate(userToken ? 'App' : 'Auth');
};
// Render any loading content that you like here
render() {
return (
<View>
<Text>Loading</Text>
<ActivityIndicator />
<StatusBar barStyle="default" />
</View>
);
}
}
认证导航器:
import { createStackNavigator } from 'react-navigation';
import LoginScreen from '../../screens/auth/LoginScreen';
import SignUpScreen from '../../screens/auth/SignUpScreen';
export default createStackNavigator({
Login : {
screen : LoginScreen
},
SignUp: {
screen : SignUpScreen
}
},
{
initialRouteName: 'Login',
})
登录屏幕:
import React, { Component } from 'react';
import { View, Text, StyleSheet } from 'react-native';
export default class LoginScreen extends Component {
render() {
console.log('LOGIN SCREEN')
return (
<View style={styles.container}>
<Text style={styles.text}>Login</Text>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
alignItems: 'center',
justifyContent: 'center',
},
text : {
fontSize: 20,
}
});
我找不到错误,但我知道它来自 AuthNavigator 和 AppNavigator。如果我将 AuthLoading 路由设置为在控制台中呈现 App 但屏幕上没有任何内容,我可以看到“登录屏幕”或“应用屏幕”,当我在 MainNavigator 中启动新路由 LoginScreen 并从 AuthLoading 导航到该路由时作品,我的屏幕上显示了一个漂亮的文字。
我知道这可能是一个微不足道的错误,但我花了几个小时自己解决这个问题并且找不到,所以如果有人可以帮助我,那就太好了!
提前感谢!