React Native组件导航失效:ListComponent点击无法打开第三页
Hey there, let's figure out why your onPress in ListComponent isn't taking you to the third screen. Since you mentioned you've updated the component and tried some fixes already, let's walk through the most common issues step by step:
Double-check your navigation setup in App.js
First off, make sure your App.js has a properly configured navigation container (like React Navigation's Stack Navigator) and that your third screen component is registered in the navigation stack. Your provided App.js code cuts off, so ask yourself:- Did you import the third screen component?
- Did you add it to the stack with a clear route name?
Here's what a correct stack setup should look like:
import ThirdScreen from './ThirdScreen'; import { NavigationContainer } from '@react-navigation/native'; import { createStackNavigator } from '@react-navigation/stack'; const Stack = createStackNavigator(); function App() { return ( <NavigationContainer> <Stack.Navigator> <Stack.Screen name="Home" component={HomeScreen} /> <Stack.Screen name="List" component={ListComponent} /> <Stack.Screen name="Third" component={ThirdScreen} /> {/* This line is critical */} </Stack.Navigator> </NavigationContainer> ); }Ensure ListComponent has access to the navigation object
If ListComponent is a screen in your navigation stack, it should receive thenavigationprop automatically. But if it's a child component nested inside another screen, you'll need to pass the navigation prop down or use theuseNavigationhook (for functional components):
For functional components:import { useNavigation } from '@react-navigation/native'; function ListComponent() { const navigation = useNavigation(); const handlePress = () => { navigation.navigate('Third'); // Match the route name from your stack! }; return ( <TouchableOpacity onPress={handlePress}> <Text>Open Third Screen</Text> </TouchableOpacity> ); }For class components:
class ListComponent extends Component { handlePress = () => { this.props.navigation.navigate('Third'); }; render() { return ( <TouchableOpacity onPress={this.handlePress}> <Text>Open Third Screen</Text> </TouchableOpacity> ); } }Watch for typos in
onPress
You mentioned using "On Press" – note that React Native uses camelCase, so it'sonPress(lowercase 'o' and 'p'). A typo here means the press event won't fire at all!Use the right touchable component
RegularVieworTextcomponents don't respond toonPressby default. You need to wrap them in a touchable component likeTouchableOpacity,TouchableHighlight, orPressable:
❌ Wrong:<Text onPress={handlePress}>Go to Third Page</Text>✅ Correct:
import { TouchableOpacity, Text } from 'react-native'; <TouchableOpacity onPress={handlePress}> <Text>Go to Third Page</Text> </TouchableOpacity>Verify the route name matches exactly
The string you pass tonavigation.navigate()must match thenameproperty you set for the third screen in your navigation stack – it's case-sensitive! For example, if your stack hasname="ThirdScreen", you need to callnavigation.navigate('ThirdScreen'), not'Third'.Debug if the press event is even firing
Add aconsole.logto your press handler to check if the event is triggering:const handlePress = () => { console.log('Press event detected!'); navigation.navigate('Third'); };If you don't see this log in your console, the issue is with how you're binding the
onPressevent. If you do see it but still don't navigate, the problem is in your navigation configuration or route name.
内容的提问来源于stack exchange,提问作者Ramazan Gül

