Skip to main content

Lesson 6 of 10

Navigation Hooks - useNavigation, useFocusEffect, useIsFocused

React Navigation's hooks let you access navigation state and respond to focus changes anywhere in your component tree - not just in screen components.

20 minBy Priygop Editorial TeamUpdated 2026Peer Reviewed & Verified

Navigation Hooks Reference

Navigation Hooks Reference
import {
  useNavigation,
  useRoute,
  useFocusEffect,
  useIsFocused,
  useNavigationState,
} from '@react-navigation/native';
import { useCallback, useRef, useState, useEffect } from 'react';

// ---- useNavigation - access navigator from any component ----
function BackButton() {
  const navigation = useNavigation();
  // No need to pass navigation as a prop through component tree
  return (
    <Pressable onPress={() => navigation.goBack()}>
      <Text>← Back</Text>
    </Pressable>
  );
}

// ---- useFocusEffect - run when screen gains/loses focus ----
// Use this instead of useEffect for screen-level side effects
// e.g. refresh data when returning from a child screen
function ProfileScreen() {
  const [profile, setProfile] = useState(null);

  useFocusEffect(
    useCallback(() => {
      let isActive = true;
      fetchProfile().then(data => { if (isActive) setProfile(data); });

      return () => {
        isActive = false;  // cleanup if screen loses focus before fetch resolves
        // cancelAll ongoing requests here
      };
    }, [])  // empty deps = runs every time screen is focused
  );

  return <View>{/* render profile */}</View>;
}

// ---- useIsFocused - for conditional rendering based on focus ----
function CameraScreen() {
  const isFocused = useIsFocused();
  // Heavy components only mount while the screen is focused
  return <View>{isFocused ? <Camera /> : null}</View>;
}

// ---- useNavigationState - read current nav state ----
function NavDebugger() {
  const currentRoute = useNavigationState(state => state.routes[state.index].name);
  return <Text>Current: {currentRoute}</Text>;
}

// ---- Prevent going back (e.g. during file upload) ----
function UploadScreen({ navigation }: NativeStackScreenProps<..., 'Upload'>) {
  const isUploading = useUploadStatus();

  useEffect(() => {
    const unsubscribe = navigation.addListener('beforeRemove', (e) => {
      if (!isUploading) return; // allow if not uploading
      e.preventDefault();       // block back navigation
      Alert.alert('Cancel upload?', 'Upload in progress will be lost.', [
        { text: 'Stay', style: 'cancel' },
        { text: 'Leave', style: 'destructive', onPress: () => navigation.dispatch(e.data.action) },
      ]);
    });
    return unsubscribe;
  }, [navigation, isUploading]);
}

async function fetchProfile() { return {}; }
function useUploadStatus() { return false; }
function Camera() { return null; }

Tip

Tip

Practice Navigation Hooks useNavigation useFocusEffect useIsFocused in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.

Diagram
Loading diagram…

useNavigation, useRoute, and useFocusEffect lifecycle hooks

Practice Task

Note

Practice Task - (1) Write a working example of Navigation Hooks useNavigation useFocusEffect useIsFocused from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.

Quick Quiz

Common Mistake

Warning

A common mistake with Navigation Hooks useNavigation useFocusEffect useIsFocused is skipping edge case testing - empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready react native code.

Topics in This Module

0/10 done