【问题标题】:Problem to return component from StackNavigator in react-native. Get a blank screen but cosole.log is OK在 react-native 中从 Stack Navigator 返回组件的问题。得到一个空白屏幕,但 console.log 没问题
【发布时间】:2019-04-19 09:12:50
【问题描述】:

我想要做的是在我的应用程序的最顶部包装一个名为 MainNavigator 的切换导航器,该导航器重定向到 LoadingScreen,它确定用户是否已记录并导航到 AuthNavigator 或 AppNavigator。 AuthNavigator 自己是一个 stackNavigator,他显示屏幕“LoginScreen”或“SignUpScreen”。 AppNavigator 是一个具有多个屏幕(Profile、Home...)的 DrawerNavigator。

我遇到的问题是返回方法似乎有效,因为我可以从“LoginScreen”组件看到控制台中的日志,但在屏幕上,唯一的返回是白屏。当我直接调用我的LoadingScreen 组件中的“LoginScreen”组件,使用:

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

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。如果我将 AuthLoading 路由设置为在控制台中呈现 App 但屏幕上没有任何内容,我可以看到“登录屏幕”或“应用屏幕”,当我在 MainNavigator 中启动新路由 LoginScreen 并从 AuthLoading 导航到该路由时工作,我的屏幕上显示了一个漂亮的文字。

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

提前致谢!

【问题讨论】:

    标签: reactjs react-native react-navigation react-navigation-stack react-navigation-drawer


    【解决方案1】:

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

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

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

    【讨论】:

    • 我在 android 设备上仍然面临同样的问题,它在 iOS 设备上运行良好,但在 android 设备上却没有。当从抽屉的另一个屏幕回到同一屏幕时,屏幕变为白色。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-01-15
    • 1970-01-01
    • 2016-03-07
    • 2018-01-03
    • 2023-04-09
    • 2021-01-27
    • 1970-01-01
    相关资源
    最近更新 更多