Sub topic 12
Showing pending state with useNavigation
This chapter assumes you can write a route module with a loader and an action, and that you have used Form to submit data. You should also know basic React state and conditional rendering.
Why pending state matters
In framework mode, a link click or form submit starts a navigation. React Router calls the matching loaders and actions on the server, and the old page stays on screen until the new data arrives. Without feedback, the app looks frozen on a slow network.
The useNavigation hook reports what the router is doing right now. It needs no state of your own, because the router tracks it for you.
The navigation object
useNavigation() returns an object with these main fields:
| Field | Meaning |
|---|---|
| `state` | `"idle"`, `"loading"`, or `"submitting"` |
| `location` | The URL being navigated to, or `undefined` when idle |
| `formData` | The submitted `FormData` during a submission, or `undefined` |
| `formMethod` | The HTTP method of the submission, such as `"POST"` |
The three states follow one cycle.
A GET form behaves like a link: it goes straight to loading. Only non-GET forms (post, put, patch, delete) enter submitting.
Disabling a submit button
Use state === "submitting" to guard against double submits. This is the pattern from the contact form:
import { Form, redirect, useNavigation } from "react-router";
import type { Route } from "./+types/new";
import { createContact } from "~/contacts.server";
export async function action({ request }: Route.ActionArgs) {
const formData = await request.formData();
const name = String(formData.get("name") ?? "").trim();
const contact = await createContact({ name });
return redirect(`/contacts/${contact.id}`);
}
export default function NewContact() {
const navigation = useNavigation();
const saving = navigation.state === "submitting";
return (
<Form method="post">
<input name="name" />
<button type="submit" disabled={saving}>
{saving ? "Saving..." : "Save"}
</button>
</Form>
);
}A global loading indicator
Because useNavigation reads router-wide state, you can call it in root.tsx and show one indicator for every navigation. Mark the content as busy and dim it.
import { Outlet, useNavigation } from "react-router";
export default function App() {
const navigation = useNavigation();
const busy = navigation.state !== "idle";
return (
<div aria-busy={busy} style={{ opacity: busy ? 0.6 : 1 }}>
<Outlet />
</div>
);
}Common errors
- Checking
"loading"for a form submit. The button stays enabled during the action, because the state is"submitting"until the action returns. Check both states if you want the button disabled for the whole round trip. - Reading
navigation.locationwhen idle. It isundefined, so use optional chaining. - Showing the indicator on every revalidation. After an action, the state becomes
"loading"while loaders re-run. A global spinner will flash for submissions too. Comparenavigation.location?.pathnameto the current path if you only want it for page changes. - Using
useNavigationfor in-place actions. It reflects the single active navigation. A button that deletes a row without leaving the page needs a fetcher, covered in a later chapter.
Exercise
A user clicks a link to a route with a slow loader. Which navigation.state values appear, in order? What changes if the same navigation comes from a <Form method="post"> whose action redirects to that route?
Show answer
For the link: "idle", then "loading" while the loader runs, then "idle". For the POST form: "idle", then "submitting" while the action runs, then "loading" while the redirect target's loader runs, then "idle". During "submitting", navigation.formData and navigation.formMethod are available.
Exercise
Write a condition that shows a spinner only while navigating to a different pathname, not while loaders revalidate after an action on the same page. Assume location comes from useLocation().
Show answer
const navigating = navigation.state === "loading" && navigation.location?.pathname !== location.pathname; Revalidation after an action on the same page has the same pathname, so navigating stays false.