我正在react native上开发一个应用程序,当我添加-“const App”时,我得到了这个错误-“找不到导航对象。您的组件在NavigationContainer中吗?”
import React from 'react';
import { TouchableOpacity, Image } from 'react-native';
import { StyleSheet, Text, View, StatusBar } from 'react-native';
import { NavigationContainer, useNavigation } from "@react-navigation/native";
import { LinearGradient } from 'expo-linear-gradient';
const App = () => {
const navigation = useNavigation()
return (
<LinearGradient
colors={['#FFFFFF', '#FFFFFF']}
style={styles.container}>
<Image
source={require('./listen.png')}
style={{height: 230, width: 412}}
/>
<NavigationContainer>
<TouchableOpacity
onPress={() => navigation.navigate('Home')}
style={styles.button}>
<Image
source={require("./Button.png")}/>
</TouchableOpacity>
</NavigationContainer>
<Text style={styles.title}>
My Text
</Text>
<Text style={styles.title2}>
My Text
</Text>
<Text style={styles.podtitle}>
My Text
</Text>
<Text style={styles.podtitle2}>
My Text
</Text>
</LinearGradient>
);
};
export default App;
我试了很多不同的组合,都不起作用。
1条答案
按热度按时间rks48beu1#
useNavigation仅当组件位于NavigationContainer内部时才有效