react-native使用 react-navigation 始終顯示空白的原因

昨天新開始了一個react-native的項目,使用了react-navigation, 久了沒用react-navigation,還是踩了坑,記錄下:
react-navigation導(dǎo)入的頁面一直顯示空白,檢查了很久,沒發(fā)現(xiàn)什么不對, 后面懷疑是不是上層的組件有問題, 果不其然, 將上層UI flex設(shè)為1 就顯示出內(nèi)容了.
但是如果不渲染<AppNavigator/>,而改成<Text>hello world</Text>, 不加flex=1也是沒有問題的, 這個就有點坑爹了, 當(dāng)時正是因為先用<Text>hello world</Text>渲染了一下, 所以判定這里是沒有問題的. 而換成AppNavigator 之后就打死都不行了. 詳見App.js代碼

index.js

//index.js
/**
 * @format
 */

import {AppRegistry} from 'react-native';
import App from './src/App';
import {name as appName} from './app.json';

AppRegistry.registerComponent(appName, () => App);

./src/App.js

./src/App.js
import React from 'react'
import {View,Text} from 'react-native'

import { createAppContainer, createStackNavigator } from 'react-navigation'


class Home extends React.Component{
    render(): boolean | number | string | React$Element<*> | React$Portal | Iterable | null {
        return (
            <View>
                <Text>this is homepage</Text>
            </View>
        )
    }
}

const Navigator = createStackNavigator({
    Home:{
        screen:Home,
    }
},
    {
        initialRouteName: 'Home'
    })

const AppNavigator = createAppContainer(Navigator)


export default class App extends React.Component{
    render():{
        return ( //可以正常顯示
           <View style={{flex:1}}>
                <AppNavigator/>
            </View>
        )
       return ( //也可以正常顯示
             <View>
               <Text>Hello world</Text>
            </View>
            )
         return ( //顯示空白, 不能顯示出this is homepage,  這個就是我折騰了兩個小時的現(xiàn)象
             <View>
                <AppNavigator/>
            </View>
            )
   }
}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容