You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

React Native导航栈页面空白问题排查与解决求助

React Native导航栈中页面空白,但直接引入组件正常显示的问题解决

我在React Native项目中添加导航功能后,页面显示空白,但导航栈中的console.log能正常输出。直接在app.tsx中引入ContinueScreen可正常显示,将其放入MainStack中则无法显示。相关代码及配置如下:

MainStack代码

import React, { useEffect, useState, useRef } from "react";
import { NavigationContainer } from "@react-navigation/native";
import { createNativeStackNavigator } from "@react-navigation/native-stack";
import "react-native-gesture-handler";

import BottomTabNavigator from "./BottomTabNavigator";
import ContinueScreen from "../screens/ContinueScreen";

const Stack = createNativeStackNavigator();

function MainStack() {
  console.log("jk");
  return (
    <NavigationContainer>
      <Stack.Navigator initialRouteName="ContinueScreen">
        <Stack.Screen name="ContinueScreen" component={ContinueScreen} />
        <Stack.Screen
          name="BottomTabNavigator"
          component={BottomTabNavigator}
        />
      </Stack.Navigator>
    </NavigationContainer>
  );
}

export default MainStack;

package.json配置

{
  "name": "app",
  "version": "1.0.0",
  "main": "node_modules/expo/AppEntry.js",
  "scripts": {
    "start": "expo start",
    "android": "expo start --android",
    "ios": "expo start --ios",
    "web": "expo start --web"
  },
  "dependencies": {
    "@react-native-async-storage/async-storage": "1.17.11",
    "@react-navigation/bottom-tabs": "^6.5.2",
    "@react-navigation/native": "^6.1.1",
    "@react-navigation/native-stack": "^6.9.7",
    "expo": "~48.0.18",
    "expo-font": "^11.1.1",
    "expo-status-bar": "~1.4.4",
    "formik": "^2.4.1",
    "nativewind": "^2.0.11",
    "react": "18.2.0",
    "react-native": "0.71.8",
    "react-native-gesture-handler": "~2.8.0",
    "react-native-safe-area-context": "4.4.1",
    "react-native-screens": "~3.18.0",
    "tailwind-react-native-classnames": "^1.5.1",
    "yup": "^1.2.0"
  },
  "devDependencies": {
    "@babel/core": "^7.20.0",
    "@types/react": "~18.0.14",
    "@types/react-native": "^0.72.2",
    "tailwindcss": "^3.3.2",
    "typescript": "^4.9.4"
  },
  "private": true
}

app.tsx代码

import { StatusBar } from 'expo-status-bar';
import { StyleSheet, Text, View } from 'react-native';
// import tw from 'tailwind-react-native-classnames';
import OnboradingScreen from './app/screens/OnboradingScreen';
import ContinueScreen from './app/screens/ContinueScreen';
import BottomTabNavigator from './app/stacks/BottomTabNavigator';
import MainStack from './app/stacks/MainStack';

export default function App() {
  return (
    <View>
     {/* <OnboradingScreen /> */}
     <MainStack/>
    </View>
  );
}

const styles = StyleSheet.create({
  // container: {
  //   flex: 1,
  //   backgroundColor: '#fff',
  //   alignItems: 'center',
  //   justifyContent: 'center',
  // },
});

问题解决方法

核心问题是app.tsx中外层<View>未设置flex:1,导致NavigationContainer无法占据整个屏幕空间,进而页面显示空白。

修复步骤:

  1. 修改app.tsx的外层<View>,添加flex:1样式:
export default function App() {
  return (
    <View style={{ flex: 1 }}>
      <MainStack />
    </View>
  );
}

或者启用你已定义的container样式:

export default function App() {
  return (
    <View style={styles.container}>
      <MainStack />
    </View>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: '#fff',
    alignItems: 'center',
    justifyContent: 'center',
  },
});
  1. 额外优化:将react-native-gesture-handler的导入移至app.tsx的最顶部,避免手势相关的潜在问题:
import "react-native-gesture-handler";
import { StatusBar } from 'expo-status-bar';
import { StyleSheet, Text, View } from 'react-native';
// ...其余导入代码

完成以上修改后,导航栈中的页面应该能正常显示。

内容的提问来源于stack exchange,提问作者Curtis Crentsil

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.19 21:49:56