Courses / React 19: From Components to Production / Chapter 6
Module 3 · Routing and React 19 · Chapter 6 of 30
React Router: Routes, Layouts, Loaders and Navigation
Builds on State and Effects: useState, Events, useEffect and Cleanup. Next up: New in React 19: Actions, use, Optimistic UI and ref as a Prop.
Part 1 · Client-side routing and the three modes
What a router does
An inter-city bus booking site has a search page, a list of trips, a seat map and a checkout, and each needs its own address so riders can bookmark it, share it and use the Back button. A router is the part of a React app that connects addresses to screens.
On a traditional site, every address is a request to a server, which answers with a new HTML page. In a single-page app the browser loads one page, and JavaScript takes over. Client-side routing means a library reads the address, picks the component that matches, renders it, and updates the address bar with the browser's History API, all without a full page load. The Back and Forward buttons are handled the same way: the router listens for them and renders the matching screen.
The routing library for React is React Router. This chapter uses version 8.5.1 with TypeScript. Everything it uses is imported from react-router, except RouterProvider. The package exports a RouterProvider from both react-router and react-router/dom. The one in react-router/dom is wired to react-dom (it hands the router flushSync), and it is the one the data mode setup guide imports, so this chapter does the same.
The idea in its smallest form is a table that maps a path pattern to a component. A request for /trips finds the row for /trips, and the router renders that row's component. Path patterns can contain dynamic parts such as /trips/:id, which a later lesson covers.