我正在尝试使用 Expo 和 React Navigation 进行简单的路由,但它在屏幕上没有显示任何内容。
这是我的推荐码
`import * as React from 'react';
import { NavigationContainer } from '@react-navigation/native';
import { View, Text } from 'react-native';
import { createNativeStackNavigator } from '@react-navigation/native-stack';
const Stack = createNativeStackNavigator();
function Home() {
console.log('Home');
return (
<View>
<Text>Home</Text>
</View>
);
}
function MyStack() {
return (
<Stack.Navigator>
<Stack.Screen name="Home" component={Home} />
</Stack.Navigator>
);
}
export default function Navigation() {
return (
<NavigationContainer>
<MyStack />
</NavigationContainer>
);
}`
这是我的 App.js 代码
import 'react-native-gesture-handler';
import { StatusBar } from 'expo-status-bar';
import { StyleSheet, Text, View } from 'react-native';
import Navigation from './src/navigation/Navigation';
export default function App() {
return (
<View style={styles.container}>
<Navigation />
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: 'red',
alignItems: 'center',
justifyContent: 'center',
},
});
在这段简短的代码中,Home 组件中的 console.log() 运行并将 Home 打印到控制台,但屏幕上没有显示任何内容。
下面是package.json文件的内容
{
"name": "new-dictionary",
"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-navigation/native": "^6.1.9",
"@react-navigation/native-stack": "^6.9.17",
"@react-navigation/stack": "^6.3.20",
"expo": "~49.0.15",
"expo-status-bar": "~1.6.0",
"react": "18.2.0",
"react-native": "0.72.6",
"react-native-gesture-handler": "~2.12.0",
"react-native-reanimated": "~3.3.0",
"react-native-safe-area-context": "4.6.3",
"react-native-screens": "~3.22.0"
},
"devDependencies": {
"@babel/core": "^7.20.0"
},
"private": true
}
如果你能帮助我,我会很高兴。谢谢
文字似乎被 SafeAreaView 的间隙隐藏了。 如果您像这样更新主屏幕,那么您就会看到。
function Home() {
console.log('Home');
return (
<View style={{flex: 1, alignItems: 'center', justifyContent: 'center'}}>
<Text>Home</Text>
</View>
);
}