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

React Navigation报错:Property 'navigation'不存在,JS引擎:Hermes

解决React Navigation中"Property 'navigation' doesn't exist"报错

以下是几种常见的排查和解决方法:

  • 确保组件是导航器注册的屏幕
    只有直接在React Navigation导航器(如StackNavigator、TabNavigator)中注册的组件,才能自动获取navigation prop。如果是嵌套的子组件,需要手动传递navigation,或者用钩子/高阶组件获取。
    示例代码:

    import { createStackNavigator } from '@react-navigation/stack';
    import { NavigationContainer } from '@react-navigation/native';
    
    const Stack = createStackNavigator();
    
    function App() {
      return (
        <NavigationContainer>
          <Stack.Navigator>
            {/* 正确注册屏幕组件 */}
            <Stack.Screen name="Home" component={HomeScreen} />
          </Stack.Navigator>
        </NavigationContainer>
      );
    }
    
  • 函数组件使用useNavigation钩子
    非直接屏幕的函数组件,通过useNavigation钩子获取导航对象:

    import { useNavigation } from '@react-navigation/native';
    
    function ChildComponent() {
      const navigation = useNavigation();
      return (
        <Button onPress={() => navigation.navigate('Details')} title="跳转至详情页" />
      );
    }
    
  • 类组件使用withNavigation高阶组件
    类组件若不是直接屏幕,用withNavigation包装组件注入navigation prop:

    import { withNavigation } from '@react-navigation/compat';
    import React from 'react';
    import { Button } from 'react-native';
    
    class ChildComponent extends React.Component {
      render() {
        return (
          <Button onPress={() => this.props.navigation.navigate('Details')} title="跳转至详情页" />
        );
      }
    }
    
    export default withNavigation(ChildComponent);
    
  • TypeScript环境下添加类型声明
    使用TypeScript时,需为组件props添加正确的导航类型,避免类型检查报错:

    import { StackNavigationProp } from '@react-navigation/stack';
    
    // 定义根栈参数列表
    type RootStackParamList = {
      Home: undefined;
      Details: undefined;
    };
    
    type HomeScreenProps = {
      navigation: StackNavigationProp<RootStackParamList, 'Home'>;
    };
    
    function HomeScreen({ navigation }: HomeScreenProps) {
      return <Button onPress={() => navigation.navigate('Details')} title="跳转" />;
    }
    
  • 检查依赖版本兼容性
    确保@react-navigation/native、对应导航器包(如@react-navigation/stack)以及react-native-screens、react-native-safe-area-context等依赖版本匹配,版本不匹配可能导致prop缺失问题。

内容的提问来源于stack exchange,提问作者Md.Mominul Islam

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.26 22:52:01