Sub topic 2
Setting up the router
This chapter assumes you can write React function components and JSX, use ES modules, and have a project running with Vite or a similar bundler. By the end, you will have a working data router that renders a component for each URL.
Installing the package
React Router's web package is react-router-dom. Install it in your project:
npm install react-router-domThe examples in this tutorial use the data router API, which is built around createBrowserRouter. Older tutorials use <BrowserRouter> with <Routes>. That API still works, but it does not support loaders, actions, or route-level error handling, which later chapters cover.
The two pieces
Setup needs two things:
createBrowserRouter(routes)takes an array of route objects and returns a router. The router holds the route list, the current location, and the navigation state. It uses the browser History API to read and update the URL.<RouterProvider router={router} />is a component that puts the router into React. It renders the component for whichever route matches the current URL. It also makes the router available to hooks such asuseParamsanduseNavigate.
A minimal working router
Each route object needs a path and an element. The element is the JSX that renders when the path matches. Put the router in App.jsx:
import { createBrowserRouter, RouterProvider } from "react-router-dom";
const router = createBrowserRouter([
{ path: "/", element: <h1>Home</h1> },
{ path: "/about", element: <h1>About</h1> },
]);
export default function App() {
return <RouterProvider router={router} />;
}Then mount App as usual in main.jsx:
import React from "react";
import { createRoot } from "react-dom/client";
import App from "./App";
createRoot(document.getElementById("root")).render(<App />);Open / and you see "Home". Open /about and you see "About". Typing a URL that matches no route shows React Router's default error page. A later chapter replaces it with your own 404 page.
Create the router once
Call createBrowserRouter at module level, outside any component, as in the example above. If you call it inside a component, every render builds a new router. That discards the current navigation state and can cause loops or flicker.
If you need the router in a place with no components, such as a test, you can export it from its own module. Chapter 17 covers tests.
Common errors
- A hook fails with a message about a missing router context. The component using
useParams,Link, or a similar hook is rendered outsideRouterProvider. Render it as a route element or as a descendant of one. - The page works from
/but returns a 404 from the server when you reload/about. The browser asks your server for/about, and the server has no such file. Configure the server to returnindex.htmlfor unknown paths. The Vite dev server already does this, but many production hosts do not. - The route list has no match for
/. Nothing renders there. Add a route withpath: "/".
Exercise
Why should createBrowserRouter be called outside your App component, and what does RouterProvider do with the result?
Show answer
If you call it inside App, every render creates a new router and loses the current navigation state. Calling it once at module level avoids that. RouterProvider takes the router, renders the element of the route that matches the current URL, and makes the router available to routing hooks and components.