在react-native中从StackNavigator返回组件的问题。得到一个空白的屏幕,但console.log是好的

问题描述 投票:0回答:1

我想要做的是在我的应用程序的顶部设置一个名为MainNavigator的切换导航器,该导航器重定向到一个LoadingScreen,用于确定用户是否为Logged或者是否导航到AuthNavigator或AppNavigator。 AuthNavigator本身就是一个stackNavigator,它显示屏幕'LoginScreen'或'SignUpScreen'。 AppNavigator是一个具有多个屏幕的DrawerNavigator(Profile,Home ...)。

我遇到的问题是返回方法似乎有效,因为我可以从“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',
  }

  ));

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>
    );
  }
}

AuthNavigator:

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。我可以看到'LOGIN SCREEN'或'APP SCREEN',如果我在我的控制台中设置AuthLoading路线来渲染App但屏幕上没有任何东西,当我在MainNavigator中初始化一个新的路径LoginScreen并从AuthLoading导航到该路线时工作,我有一个漂亮的文字显示在我的屏幕上。

我知道这可能是一个不起眼的错误,但我花了几个小时自己解决这个问题并且找不到,所以如果有人可以帮助我,那就太好了!

谢谢提前!

reactjs react-native react-navigation react-navigation-stack react-navigation-drawer
1个回答
0
投票

问题解决了......因为在App.js中我的应用程序的顶部,我包裹了我的MainNavigator

<View>
<StatusBar hidden={true} /> 
<MainNavigator />
</View>

所以我删除了视图和状态栏...它的工作原理。 4天才搞清楚......我喜欢它! :')

© www.soinside.com 2019 - 2024. All rights reserved.