Course contentsShow
← React
Lesson 1851 of 2,39643. RoutingFree lesson

useNavigation: Pending States Made Easy

Tracking navigation state (idle, loading, submitting) globally without manual loading flags.

useNavigation: Pending States Made Easy

What you'll learn: How to track navigation state globally across your app without manually managing loading flags in every component.

The Global Navigation State Problem

When using data routers (like createBrowserRouter), navigations triggered by loaders, actions, or <Form> submissions happen at the router level. You used to need loading states in each component, but React Router provides a better way: useNavigation gives you a single source of truth for what's happening across your entire navigation system.

How useNavigation Works

The useNavigation hook returns an object describing the current navigation state:

import { useNavigation } from 'react-router-dom';

function GlobalLoadingIndicator() {
  const navigation = useNavigation();
  
  // navigation.state can be:
  // "idle" - nothing happening
  // "loading" - navigating to a route (loader running)
  // "submitting" - form being submitted (action running)
  
  if (navigation.state === "loading") {
    return <div className="spinner">Loading...</div>;
  }
  
  if (navigation.state === "submitting") {
    return <div className="spinner">Saving...</div>;
  }
  
  return null;
}

Think of It Like a Traffic Light

Your router is the traffic controller. Instead of each intersection (component) having its own lights, there's one central system broadcasting the current state to everyone who needs it. Any component can "look up" and see: are we moving? stopped? turning?

You can place this indicator anywhere—typically in your root layout—and it automatically reflects loader fetches, action submissions, and navigation transitions without props, context, or manual coordination.

Key Takeaway: useNavigation provides a global view of router activity (idle, loading, submitting), eliminating the need for component-level loading flags when working with data routers.