React Native 我遇到此错误-“找不到导航对象,您的组件是否在NavigationContainer中?”

0lvr5msh  于 2022-11-17  发布在  React
关注(0)|答案(1)|浏览(150)

我正在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;

我试了很多不同的组合,都不起作用。

rks48beu

rks48beu1#

useNavigation仅当组件位于NavigationContainer内部时才有效

<NavigationContainer>
      <Stack.Navigator>
        <Stack.Screen name="Home" component={App} />
      </Stack.Navigator>
</NavigationContainer>

相关问题