Sub topic 12
Showing pending and submitting states
This chapter assumes you can define routes with loaders and actions and use Form, as covered in the earlier chapters. Here you will show feedback while the router works.
Why the page seems frozen
With a data router, clicking a link does not render the next page at once. The router first runs the target route's loader and waits for it. If the loader is slow, the old page stays on screen with no sign that anything is happening. A form submission behaves the same way while its action runs. You fix this by reading the router's navigation state.
The useNavigation hook
useNavigation returns an object that describes the navigation in progress. Its state property has one of three values:
| `navigation.state` | Meaning |
|---|---|
| `"idle"` | No navigation is in progress. |
| `"loading"` | Loaders are running for the next page. This also covers loaders that revalidate after an action. |
| `"submitting"` | A `Form` was submitted and its action is running. |
The hook works in any component under RouterProvider. A layout route is the usual place for it, because the layout stays mounted while the child route changes.
A global loading indicator
This layout dims the page and shows a status message while any loader runs.
import { Link, Outlet, useNavigation } from "react-router-dom";
export function Layout() {
const navigation = useNavigation();
const isLoading = navigation.state === "loading";
return (
<div>
<nav>
<Link to="/">Home</Link> <Link to="/contacts">Contacts</Link>
</nav>
{isLoading && <p role="status">Loading...</p>}
<main style={{ opacity: isLoading ? 0.5 : 1 }}>
<Outlet />
</main>
</div>
);
}Keeping the old page visible but dimmed usually feels better than replacing it with a blank spinner screen.
Pending state for a form
The form from the actions chapter already uses navigation.state === "submitting" to disable its button. While submitting is true, you can also read what the user sent through navigation.formData. It is undefined when no submission is in progress.
import { Form, useNavigation } from "react-router-dom";
export function NewContact() {
const navigation = useNavigation();
const busy = navigation.state === "submitting";
const sentName = navigation.formData?.get("name");
return (
<Form method="post">
<input name="name" required />
<button type="submit" disabled={busy}>
{busy ? `Saving ${sentName}...` : "Save"}
</button>
</Form>
);
}Disabling the button also prevents a double submit, which is a common source of duplicate records.
Common mistakes
- Checking only for
"loading"on a form page. The first phase of a submission is"submitting", so a spinner tied to"loading"appears late. - Calling
useNavigationin a component outside the router. It needsRouterProviderabove it. - Putting the indicator in a child route. The child unmounts when the URL changes, so put the indicator in a layout that stays mounted.
- Expecting
navigation.stateto change on the initial page load. It stays"idle"until the first navigation.
Exercise
A user clicks a Save button in a Form with method="post". The action saves the data and returns redirect("/contacts"). List the values of navigation.state in order, starting from the click.
Show answer
"submitting" while the action runs, then "loading" while the loaders for /contacts run, then "idle" once the new page renders.
Exercise
Why is a layout route a better place for a global loading indicator than the page component that has the loader?
Show answer
The layout stays mounted across navigations, so it can show the indicator while the page's loader runs. The page component is not rendered until its loader finishes, and the old page unmounts when the URL changes.