Sub topic 15
Choosing a router: browser, hash, or memory
This chapter assumes you know how createBrowserRouter and RouterProvider work, and that you can read a route object. It covers the other two data routers and how to pick one.
What a router does
A router object holds the current location and the route tree. It also knows how to read and write the location. React Router ships three data routers that differ only in where that location lives:
| Function | Location lives in | Typical use |
|---|---|---|
| `createBrowserRouter` | The URL path, through the History API | Most web apps |
| `createHashRouter` | The part of the URL after `#` | Hosts that cannot rewrite URLs |
| `createMemoryRouter` | A JavaScript array in memory | Tests, Storybook, embedded widgets |
All three take the same route array and are passed to RouterProvider. Loaders, actions, errorElement and lazy behave identically. Switching routers is a one-line change.
Browser router
URLs look like https://example.com/users/42. This is the default choice. It has one deployment requirement. If a user reloads or opens /users/42 directly, the browser asks your server for that path. The server must answer with index.html for any path that is not a real file. Without that rule, you get a 404 from the server before React loads.
Hash router
URLs look like https://example.com/#/users/42. Browsers never send the part after # to the server, so the server only ever sees /. Deep links and reloads work on static hosts without any rewrite rule.
import { createHashRouter, RouterProvider } from "react-router-dom";
const router = createHashRouter([
{ path: "/", element: <h1>Home</h1> },
{ path: "/about", element: <h1>About</h1> },
]);
export default function App() {
return <RouterProvider router={router} />;
}The costs are uglier URLs and no server-side knowledge of the path. Search engines also treat hash URLs poorly, and the # can no longer be used for in-page anchors.
Memory router
A memory router keeps its history in an array and never touches the address bar. You choose the starting entries, which makes it the right tool for tests.
import { render, screen } from "@testing-library/react";
import { createMemoryRouter, RouterProvider } from "react-router-dom";
test("renders the user page for /users/42", async () => {
const router = createMemoryRouter(
[{ path: "/users/:userId", element: <h1>User page</h1> }],
{ initialEntries: ["/users/42"] }
);
render(<RouterProvider router={router} />);
expect(await screen.findByText("User page")).toBeTruthy();
});initialEntries sets the history stack, and initialIndex picks which entry is current. Create a new router inside each test so state does not leak between tests.
Deciding
Common errors
- Reload on a nested URL shows a server 404: the browser router is deployed without a fallback to
index.html. - A test finds no content: the memory router started at
/becauseinitialEntrieswas omitted. - Mixing router types in one app: each
RouterProviderhas its own history, so links in one do not update the other.
Exercise
Your app is deployed to a static host where you cannot add rewrite rules. Visiting /about by typing it in the address bar returns a 404. Which router fixes this without touching the host, and what will the URL for /about become?
Show answer
Use createHashRouter. The browser does not send the part after # to the server, so the host only serves the root page. The URL becomes https://your-site/#/about.