javascript React导航-组件不显示屏幕

uz75evzq  于 2023-06-28  发布在  Java
关注(0)|答案(4)|浏览(119)

就像我在标题中说的,我试图在React导航中显示一个带有.Screen的组件,但由于某种原因,它没有这样做
index.js

import React from 'react'
import { StyleSheet , View, Text } from 'react-native'
import 'react-native-gesture-handler'
import { StatusBar } from 'expo-status-bar'

import { NavigationContainer } from '@react-navigation/native'
import { createStackNavigator } from '@react-navigation/stack'

import Home from './source/fragments/Home'

export default function App() {
  const AuthStack = createStackNavigator()
  return (
    <View style = { styles.container }>
      <StatusBar style = "auto" />
      <NavigationContainer>
        <AuthStack.Navigator initialRouteName = "Home">
          <AuthStack.Screen 
            name = "Home"
            component = {Home} 
          />
        </AuthStack.Navigator>
      </NavigationContainer>
    </View>
  )
}
const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: '#ffff',
    alignItems: 'center'
  }
})

Home.js

import React, { Component } from 'react'
import { View, Text } from 'react-native'

export default class Home extends Component {
    render() {
        return (
            <View>
                <Text> Home screen </Text>
            </View>
        )
    }
}

谢谢你在这里滚动

6g8kf2rb

6g8kf2rb1#

下面的答案都不是我的解决方案,我通过删除index.js中的<View>组件来修复它
就像下面的代码:index.js

export default function App() {
  const Stack = createStackNavigator()
  return (
    <NavigationContainer>
      <StatusBar style = "auto"/>
      <Stack.Navigator initialRouteName = "Home">
        <Stack.Screen 
          name = "Home"
          component = {Home}
        />
      </Stack.Navigator>
    </NavigationContainer>
  )
}
8i9zcol2

8i9zcol22#

导出主组件,如下所示

import React, { Component } from 'react'
import { Text, View } from 'react-native'

export class Home extends Component {
    render() {
        return (
            <View>
                <Text> This is Home </Text>
            </View>
        )
    }
}

export default Home
nx7onnlm

nx7onnlm3#

我也有同样的问题时,添加新的堆栈到我的导航,只是重新启动应用程序

vu8f3i0k

vu8f3i0k4#

将高度设置为组件:

import React from 'react'
import { StyleSheet , View, Text, Dimensions} from 'react-native'
import 'react-native-gesture-handler'
import { StatusBar } from 'expo-status-bar'

import { NavigationContainer } from '@react-navigation/native'
import { createStackNavigator } from '@react-navigation/stack'

import Home from './source/fragments/Home'

export default function App() {
  const AuthStack = createStackNavigator()
  return (
    <View style = { styles.container }>
      <StatusBar style = "auto" />
      <NavigationContainer>
        <AuthStack.Navigator initialRouteName = "Home">
          <AuthStack.Screen 
            name = "Home"
            component = {Home} 
          />
        </AuthStack.Navigator>
      </NavigationContainer>
    </View>
  )
}
const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: '#ffff',
    alignItems: 'center'
    height: Dimensions.get('screen').height
  }
})

我使用“尺寸”获取屏幕高度

相关问题