You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

React Native组件导航失效:ListComponent点击无法打开第三页

排查React Native中ListComponent onPress无法跳转第三页的问题

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 the navigation prop automatically. But if it's a child component nested inside another screen, you'll need to pass the navigation prop down or use the useNavigation hook (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's onPress (lowercase 'o' and 'p'). A typo here means the press event won't fire at all!

  • Use the right touchable component
    Regular View or Text components don't respond to onPress by default. You need to wrap them in a touchable component like TouchableOpacity, TouchableHighlight, or Pressable:
    ❌ 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 to navigation.navigate() must match the name property you set for the third screen in your navigation stack – it's case-sensitive! For example, if your stack has name="ThirdScreen", you need to call navigation.navigate('ThirdScreen'), not 'Third'.

  • Debug if the press event is even firing
    Add a console.log to 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 onPress event. 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 09:31:12