React Native导航异常:跳转Profile路由时组件类型报错咨询
Hey there, let's break down why this error is happening and fix it step by step.
What's causing the error?
The core issue here is that the component you've assigned to the Profile route in your navigator isn't a valid React component. Looking at your code snippet, the biggest red flag is how you're importing the Profile component:
import Profile from '../AwesomeProject';
../AwesomeProject points to your project's root directory, not the actual Profile component file. That means you're importing something that's definitely not a React component (like your project's entry file or a folder), so the navigator throws that error.
How to fix it:
1. Fix the Profile component import path
First, find where your Profile component is stored. For example, if you have a screens folder in your project root with Profile.js inside, your import should look like this (adjust the path to match your actual folder structure):
// If App.js is in your project root import Profile from './screens/Profile'; // If App.js is inside a src folder, and Profile is in src/screens import Profile from './screens/Profile';
Double-check the relative path: use ./ for files in the same directory, ../ to go up one level.
2. Ensure Profile is a valid, exported React component
Open your Profile component file and make sure it's set up correctly with a default export. Here's a basic example:
// Profile.js import React from 'react'; import { View, Text } from 'react-native'; // Class component example export default class Profile extends React.Component { render() { return ( <View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}> <Text>Profile Screen</Text> </View> ); } } // Or functional component example // const Profile = () => { // return ( // <View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}> // <Text>Profile Screen</Text> // </View> // ); // }; // export default Profile;
If you forget the export default, the import in App.js will be undefined, which also triggers the same error.
3. Verify your StackNavigator configuration
Make sure your navigator is correctly mapping the Profile route to the imported component, and check for typos in navigation (your code snippet had naviga... which looks like a typo):
const AppNavigator = StackNavigator({ Home: { screen: App, // Your home screen component }, Profile: { screen: Profile, // This must be the imported Profile component }, }); // Then in your App component's render method <Button style={styles.button} onPress={() => this.props.navigation.navigate('Profile')} // Correct spelling of navigation title="Go to Profile" />
Quick troubleshooting tip
If you're still stuck, add console.log(Profile) right after importing it in App.js. If the output is undefined, that confirms either your import path is wrong, or the Profile component isn't exported correctly.
内容的提问来源于stack exchange,提问作者Never Lucky

