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

使用React Navigation与Expo时遇ReferenceError:navigation变量未找到求助

Hey there, let's work through this ReferenceError: Can't find variable: navigation issue step by step—this is super common when starting out with React Navigation and Expo, so don't stress too much!

1. Make sure your components are wrapped in the navigation container

First and foremost, any component that needs access to the navigation object must be nested inside a <NavigationContainer> (from @react-navigation/native) and added as a screen in your navigator stack/tab bar. Here's a correct basic setup example:

// App.js
import { NavigationContainer } from '@react-navigation/native';
import { createStackNavigator } from '@react-navigation/stack';
import HomeScreen from './screens/HomeScreen';

const Stack = createStackNavigator();

export default function App() {
  return (
    <NavigationContainer>
      <Stack.Navigator>
        <Stack.Screen name="Home" component={HomeScreen} />
      </Stack.Navigator>
    </NavigationContainer>
  );
}

If your component is outside the <NavigationContainer> or not registered as a screen in your navigator, React Navigation can't pass the navigation object to it at all.

2. Correctly access the navigation object

There are two standard ways to get the navigation object—never try to use it as a global variable directly:

Option 1: Grab it from component props

If your component is passed directly as the component prop to <Stack.Screen> (or any other navigator screen), navigation is automatically passed as a prop:

// HomeScreen.js
export default function HomeScreen({ navigation }) {
  return (
    <Button
      title="Go to Details"
      onPress={() => navigation.navigate('Details')}
    />
  );
}

Note: For class components, use this.props.navigation instead of destructuring.

Option 2: Use the useNavigation Hook

If your component is a child of a screen component (not a direct navigator screen), use the useNavigation Hook to fetch the navigation object:

// ChildComponent.js
import { useNavigation } from '@react-navigation/native';

export default function ChildComponent() {
  const navigation = useNavigation();
  return (
    <Button
      title="Go Back"
      onPress={() => navigation.goBack()}
    />
  );
}
3. Verify Expo and React Navigation version compatibility

Mismatched versions can cause weird, hard-to-debug issues. Always use Expo's install command to get compatible dependencies:

  • Install core navigation dependencies:
npx expo install @react-navigation/native react-native-screens react-native-safe-area-context
  • If you're using the stack navigator, add this too:
npx expo install @react-navigation/stack

Avoid using plain npm install for these packages—npx expo install ensures versions match your Expo SDK.

4. Do a thorough cache reset

You've tried some cache steps, but let's do a more complete reset:

  1. Delete node_modules and lock files:
rm -rf node_modules package-lock.json yarn.lock
  1. Clear Expo's build cache:
npx expo start --clear
  1. Reinstall all dependencies with Expo's command:
npx expo install
5. Double-check for typos or scope issues
  • Make sure you didn't misspell navigation (e.g., navigtion is a common typo)
  • Ensure you're using navigation within the correct scope—don't try to call it outside a component function or in a place where it hasn't been properly fetched.

If none of these fix the issue, sharing your navigation setup code and the component where the error occurs will help pinpoint the problem faster!

内容的提问来源于stack exchange,提问作者Mohsen Soleimani

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.19 10:07:31