Skip to content
Tutorials Center
My learning

Topics

Lesson 19 of 39 ยท Actions and Forms

Fetchers and Concurrent Forms

Use useFetcher in React Router v7 to submit forms and load data without changing the URL, run many mutations at once, and track each one with its own state.

A normal <Form> submission is a navigation: the page's pending state is global, and only one navigation is active at a time. Many interactions are not navigations: a "like" button, a toggle, a row's inline edit, an autocomplete. For these, React Router provides fetchers. A fetcher calls a loader or an action without changing the URL, and every fetcher has its own state, so many can run at the same time.

The hook cannot run on this site, so the TSX is read-only. The stand-ins model fetcher state and concurrency with plain JavaScript.

useFetcher basics

TSX
import { useFetcher } from "react-router";

function LikeButton({ postId, liked }: { postId: string; liked: boolean }) {
  const fetcher = useFetcher();
  return (
    <fetcher.Form method="post" action={`/posts/${postId}/like`}>
      <button name="liked" value={liked ? "false" : "true"} disabled={fetcher.state !== "idle"}>
        {liked ? "Unlike" : "Like"}
      </button>
    </fetcher.Form>
  );
}
  • fetcher.Form works like <Form> but never navigates.
  • fetcher.state is "idle", "submitting" or "loading" (the loaders rerunning after the action).
  • fetcher.data holds what the action returned.
  • fetcher.formData is the in-flight submission, useful for optimistic UI (next lesson).

The fetcher posts to the route you name in action. A route that exists only to receive fetcher requests is called a resource route: it exports an action (and/or loader) but no default component.

TSX
// app/routes/posts.$id.like.tsx  (resource route: no default export)
export async function action({ request, params }: Route.ActionArgs) {
  const form = await request.formData();
  await setLiked(params.id, form.get("liked") === "true");
  return { ok: true };
}

Calling an action from code

fetcher.submit sends data without a form element, for example from an event handler:

TSX
const fetcher = useFetcher();

function onToggle(checked: boolean) {
  fetcher.submit({ done: String(checked) }, { method: "post", action: `/todos/${id}` });
}

To read data, use fetcher.load("/api/search?q=abc"). It calls the route's loader, which is how you build an autocomplete without a client-side data library.

JS Stand-in: a fetcher state machine

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Each fetcher is independent

Render a list and give every row its own fetcher. Pressing "Delete" in row 3 puts only row 3 into a pending state, and you can start several at once.

TSX
function Row({ todo }: { todo: Todo }) {
  const fetcher = useFetcher();
  const deleting = fetcher.state !== "idle";
  return (
    <li style={{ opacity: deleting ? 0.4 : 1 }}>
      {todo.title}
      <fetcher.Form method="delete" action={`/todos/${todo.id}`}>
        <button>Delete</button>
      </fetcher.Form>
    </li>
  );
}

To show a summary of all in-flight fetchers (for example a global "Saving..." badge), use useFetchers().

JS Stand-in: many independent fetchers at once

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Situation Use
Creating something, then going to its page <Form> + redirect
Search box that updates the URL <Form method="get">
Like, toggle, inline edit, delete in a list useFetcher
Autocomplete or lazy-loaded panel fetcher.load
A button in a layout that submits to another route useFetcher with action

A good rule: if the URL should change, it is a navigation; if the user stays where they are, it is a fetcher.

Revalidation still happens

After a fetcher's action finishes, the active loaders rerun, so lists and counters update by themselves. If a fetcher call should not refresh the page, set shouldRevalidate on the affected routes (use with care).

Concurrency and ordering

Fetcher submissions to the same fetcher cancel the previous one if it is still in flight; different fetchers are independent. Two actions can finish in any order, so write actions that do not depend on each other, and keep operations like "set liked to true" idempotent (running them twice gives the same result) rather than "toggle".

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What is the main difference between <Form> and fetcher.Form?

    Fetchers run actions and loaders in the background and track state per fetcher.
  2. 2. Which method calls a route's loader without navigating?

    fetcher.load(url) runs the loader of the matched route.
  3. 3. What is a resource route?

    It serves data or handles mutations and renders no UI.
  4. 4. Why prefer idempotent actions such as 'set liked to true' over 'toggle'?

    Requests can overlap or be retried, so the same input must give the same end state.

Summary

  • Fetchers run loaders and actions without navigation and keep per-fetcher state.
  • Use fetcher.Form, fetcher.submit and fetcher.load; target resource routes with action.
  • Many fetchers run concurrently; useFetchers() shows them all.
  • Revalidation still refreshes active loaders; keep mutations idempotent.

Next: Optimistic UI