liveBook · Manning

Routing

As explained in React in Depth

A route is a configured application endpoint or location that maps incoming requests to a handler or interface output.

Routing makes the URL a source of navigation in a React website framework. A route can select a page component, load data for that page, or handle an API request. In Next.js and Remix, route names are tied to file names, so the location of a file determines which URL it serves. section 11.1 section 11.3.1 section 11.3.4

Choose the route shape according to what the URL must represent:

  • Use a fixed page file for a fixed URL such as /about. For a nested URL such as /about/team, Next.js pages routing and Remix v1 use nested files; Remix v2 can use the flat file about.team.jsx. section 11.3.1
  • Use a parameterized file when many URLs share one component. A country-and-city site can serve /gb and /gb/london without creating a separate component for every place. section 11.3.1
  • Attach server-side data loading when the initial HTML needs route-specific data. Next.js pages routes pair a default-exported component with getServerSideProps; Remix routes pair a component with a loader and read its result with useLoaderData. section 11.3.2
Table 11.2 Dynamic routing in a website framework includes parameterized URLs inside the regular location.
Component location
URL
Next.js
Remix v1
Remix v2
http://localhost
/pages
/app/routes
/app/routes
/user/1
/user/2
/user/...
/user
  /[id].jsx
/user
  /$id.jsx
/user.$id.jsx
/shop/chairs/page/1
/shop/tables/page/1
/show/tables/page/2
/shop/.../page/...
/shop
- /[cat]
   - /page
    - /[page].jsx
/shop
- /$cat
  - /page
    - /$page.jsx
/shop.$cat.page.
$page.jsx

Compare the parameterized file names for user IDs and shop categories across Next.js pages routing, Remix v1, and Remix v2. The URL pattern is similar; the naming convention is not. section 11.3.1

Load a shop category from the URL

A category route extracts categoryId, fetches its data, and renders the category name. The Next.js pages example gets the parameter from context.query and returns props from getServerSideProps. The Remix example gets it from context.params, returns data from a loader, and reads that data in the component. Both examples redirect when the category is missing. section 11.3.2

// This is the code in the file    #1
// /pages/shop/[categoryId].jsx   #1
export async function
getServerSideProps(context) {    #2
  const { categoryId } = context.query;   #3
  const category =     #4
    await getCategoryData(categoryId);    #4
  if (!category) {
    return {     #5
      redirect: {    #5
        destination: "/",   #5
        permanent: false,    #5
      },   #5
    };    #5
  }
  return { props: { category } };    #6
}
function Category({ category }) {   #7
  return (   #7
    <h1>This is the page for the {category.name} category</h1>
  );
}
export default Category; #8

Inspect the Next.js pages route’s parameter lookup, server-side fetch, redirect, and prop handoff. section 11.3.2

// This is the code in the file    #1
// /app/routes/shop.$categoryId.jsx   #1
export async function loader(context) {     #2
  const { categoryId } = context.params;     #3
  const category = await getCategoryData(categoryId);
  if (!category) {
    redirect("/");    #4
  }
  return json({ category });     #5
}
function Category() {
  const { category } = useLoaderData();     #6
  return (
    <h1>This is the page for the {category.name} category</h1>
  );
}
export default Category;

Compare the Remix loader and useLoaderData handoff with the Next.js implementation. section 11.3.2

An API route serves data rather than a page. In Next.js, its file exports a handler that receives request and response objects and sets the response status and body. In Remix, a route with a loader but no component can serve as an API route. For /api/geocode, either approach reads coordinates, looks up a city and country through a service, and returns a page path or a not-found response. These are framework-specific choices, not a reason to assume one routing convention fits every project. section 11.3.4 section 11.5

How React in Depth covers it

How deep it goes
Works through fixed and parameterized file conventions, server-side data handoffs, and API handlers; does not detail routing internals.
Examples
Uses country and city URLs, shop-category data routes, and a coordinate-to-city geocoding API.
What it assumes
Assumes React components and basic URL familiarity while introducing the frameworks’ route-file and data-loading conventions.
What you can do after
Apply it via Next.js pages routing or Remix to create dynamic pages with server-loaded data and API endpoints.
How it connects
Connects parameterized URLs with server-side rendering, data loading, redirects, and API routes.

Also explained in

Compare how 34 books explain routing →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of React in Depth. The section links point to the parts of the book it draws on.