【问题标题】:Why Don't I See Anything Rendering in My Stack Navigator?为什么我在 Stack Navigator 中看不到任何渲染内容?
【发布时间】:2021-02-07 23:27:53
【问题描述】:

我有这个文件,除了屏幕顶部的标题,我什么都看不到:

import React from 'react';
import { Text, View, Button } from 'react-native';
import { NavigationContainer} from '@react-navigation/native';
import { createBottomTabNavigator } from '@react-navigation/bottom-tabs';
import { createStackNavigator } from '@react-navigation/stack'
import CreateSessionScreen from '../screens/CreateSessionScreen';
import CompletedSessionsScreen from '../screens/CompletedSessionsScreen';

const Tab = createBottomTabNavigator();
const Stack = createStackNavigator();

function Hello() {
  return (
    <View>
        <Text 
          style={{
          flex: 1, 
          fontSize: 100, 
          height: 80, 
          width: 80, 
          borderColor: 'black', 
          borderWidth: 1, 
          justifyContent: 'center',
          alignItems: 'center'}}>
            Hello World
          </Text>
          <Button title='click me'>Click me</Button>
    </View>
  );
}

function AppNavigator() {
  return (
    <NavigationContainer >
      <Stack.Navigator>
        <Stack.Screen name="Hello" component={Hello} />
        <Stack.Screen name="Completed Sessions" component={CompletedSessionsScreen} />
        <Stack.Screen name="CreateSessionScreen" component={CreateSessionScreen} />
      </Stack.Navigator>
    </NavigationContainer>
  );
}

export default AppNavigator;

由于某种原因,我看不到任何渲染到屏幕上的内容。我可以在组件中抛出一个 console.log 以确保它正在呈现。我导入了几个组件,然后将 Hello 组件放在同一个文件中进行故障排除,但都无法渲染

【问题讨论】:

  • 如果增加View 的高度,是否会显示任何内容?将 flex 和边框样式从 Text 移动到 View?我看代码的第一个想法是,iOS 标头默认为float,没有任何东西可以拉伸或定位您的视图,它可能位于标头下方
  • 试过了,没有变化:(

标签: javascript react-native react-navigation react-native-ios react-native-navigation


【解决方案1】:

问题在于您的fontSize = 100,与此相比,您的高度/宽度非常小。把它改成 10 看看会发生什么。

 function Hello() {
  return (
    <View
      style={{
        justifyContent: 'center',
        alignItems: 'center',
        flex: 1,
      }}>
      <Text>
        Hello World
      </Text>
      <Button title="click me">Click me</Button>
    </View>
  );
}

【讨论】:

  • 没有变化@Wen :(
  • 编辑了答案。将样式移动到 View 而不是 Text 组件。
猜你喜欢
  • 2020-02-06
  • 2011-02-21
  • 2016-04-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多