Sub topic 14
Fetchers and optimistic UI
This chapter assumes you have read the chapters on actions, Form, and useNavigation. You should be comfortable with route modules, action functions, and FormData.
Why fetchers exist
A Form submission is a navigation. The URL can change, useNavigation reports the state, and the page waits for the action. Some interactions should not behave that way: a "like" button, a checkbox that toggles a todo, or an inline rename. You want to call an action and revalidate loaders without leaving the page.
A fetcher does this. It is a handle from useFetcher that submits to an action or loads from a loader, and it keeps its own state, separate from the page's navigation state.
A favorite button
import { useFetcher } from "react-router";
export function FavoriteButton({
contactId,
favorite,
}: {
contactId: string;
favorite: boolean;
}) {
const fetcher = useFetcher();
const optimistic = fetcher.formData
? fetcher.formData.get("favorite") === "true"
: favorite;
return (
<fetcher.Form method="post" action={`/contacts/${contactId}/favorite`}>
<button
type="submit"
name="favorite"
value={optimistic ? "false" : "true"}
aria-label={optimistic ? "Remove from favorites" : "Add to favorites"}
>
{optimistic ? "★" : "☆"}
</button>
</fetcher.Form>
);
}fetcher.Form works like Form, but it does not navigate. The action prop points at a route that handles the request. Without it, the fetcher posts to the nearest route's action.
Reading fetcher state
| Property | Meaning |
|---|---|
| `fetcher.state` | `"idle"`, `"submitting"`, or `"loading"` (revalidating after the action) |
| `fetcher.formData` | The data being submitted, or `undefined` when idle |
| `fetcher.data` | The last value returned by the action or loader |
Optimistic UI
Optimistic UI shows the expected result before the server confirms it. In the example above, fetcher.formData exists only while a submission is in flight. While it exists, the component reads the new value from it. When the action finishes and loaders revalidate, formData becomes undefined and the component falls back to the real favorite prop from the server.
This gives a useful failure behavior. If the action fails, the next revalidation returns the old value, and the UI snaps back on its own. You do not write rollback code.
Common errors
- Using
Formfor small interactions. Every submit becomes a navigation, souseNavigationstates flip for the whole page. - Sharing one fetcher. Each
useFetcher()call is independent. Rendering a list of items with one fetcher at the top means all rows share one state. CalluseFetcherinside each row's component. - Trusting the optimistic value. It is a guess. Validate in the action as usual.
- Forgetting the action target. A fetcher without
actionposts to the current route, which may not be the one you meant.
Exercise
A todo list renders one checkbox per item, and all checkboxes show "submitting" when you toggle any one of them. What is the likely cause, and how do you fix it?
Show answer
The list calls useFetcher() once in the parent and shares that fetcher across rows, so they share state. Move the useFetcher() call into a component rendered once per row, so each row has its own fetcher and its own state and formData.