Skip to content
Tutorials Center
My learning

Topics

Lesson 12 of 39 ยท Loaders and Data Loading

Request, Params and Search Params in Loaders

Use the Request object, route params and URL search params inside React Router v7 loaders to build search, filtering and pagination that works without JavaScript.

A loader receives an object with the standard web request, the route params, and (on the server) a context you control. Together they carry everything about the incoming URL, headers and cookies. This lesson shows how to read them and use them for search, filtering and pagination, driven entirely by the URL.

The real framework code is read-only here; the stand-ins use the same Web Request and URL objects that exist in Node and browsers.

What a loader receives

TSX
import type { Route } from "./+types/products";

export async function loader({ request, params, context }: Route.LoaderArgs) {
  const url = new URL(request.url);
  const q = url.searchParams.get("q") ?? "";
  const cookie = request.headers.get("Cookie");
  // params.categoryId comes from a path like categories/:categoryId
  return { q, category: params.categoryId };
}
  • request is a standard Request: url, method, headers, signal.
  • params holds the dynamic segments (always strings or undefined).
  • context is whatever your server adapter provides, such as a database handle or the authenticated user.

Search params as state

Put filters in the URL, not in component state. Then the page can be bookmarked, shared, reloaded and crawled, and the back button works.

TSX
export async function loader({ request }: Route.LoaderArgs) {
  const url = new URL(request.url);
  const q = (url.searchParams.get("q") ?? "").trim();
  const page = Math.max(1, Number(url.searchParams.get("page")) || 1);
  const pageSize = 10;

  const { rows, total } = await searchProducts({ q, offset: (page - 1) * pageSize, limit: pageSize });
  return { q, page, pages: Math.ceil(total / pageSize), rows };
}

The matching UI is a plain GET form, which works even with JavaScript disabled:

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

<Form method="get">
  <input type="search" name="q" defaultValue={loaderData.q} />
  <button>Search</button>
</Form>

Submitting adds ?q=keyboard to the URL, the router calls the loader again, and the list updates with no manual fetch code.

JS Stand-in: a search and pagination loader

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

No output.

Notice how Number("abc") || 1 and Math.max turn bad input into a safe default. User input in a URL is untrusted, so parse and clamp every value.

Params: validate before you query

params.id is a string from the URL. Check it before using it:

TSX
export async function loader({ params }: Route.LoaderArgs) {
  const id = Number(params.id);
  if (!Number.isInteger(id) || id < 1) {
    throw new Response("Invalid id", { status: 400 });
  }
  const product = await getProduct(id);
  if (!product) throw new Response("Not found", { status: 404 });
  return { product };
}

400 means "your request is malformed", 404 means "no such thing". Distinguishing them helps clients and logs.

Headers and cookies

Headers come from request.headers. This is how a loader reads a session cookie (see Cookies and Sessions) or the Accept-Language header:

TSX
const lang = request.headers.get("Accept-Language")?.split(",")[0] ?? "en";
JS Stand-in: read a cookie from the request

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

No output.

The abort signal

request.signal aborts when the user navigates away before the loader finishes. Passing it to fetch or your database client cancels wasted work:

TSX
export async function loader({ request }: Route.LoaderArgs) {
  const res = await fetch("https://api.example.com/items", { signal: request.signal });
  return res.json();
}

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why store search text in the URL's search params?

    URL state survives reloads and links, and a GET form works without JavaScript.
  2. 2. What type is params.id in a loader?

    URL segments are text; convert and validate them yourself.
  3. 3. An id like abc was requested. Which status fits best?

    400 signals a malformed request; 404 is for well-formed ids with no matching record.
  4. 4. What is request.signal useful for?

    It is an AbortSignal that fires when the request is cancelled.

Summary

  • Loaders get request, params and context.
  • Keep filters and page numbers in search params; use a GET <Form> to change them.
  • Parse and clamp all URL input; throw 400 or 404 responses for bad or missing resources.
  • Read cookies and headers from request.headers; pass request.signal to cancellable work.

Next: Typed Loader Data