React / 39
Hash & memory routers
createHashRouter keeps the route after a
createBrowserRouter
server sees GET /menu/7
Server must send index.html for every path
createHashRouter
server sees GET / (the # part never leaves the browser)
No server setup at all
createMemoryRouter
server sees nothing: history lives in a JS array
You set the start: initialEntries: ["/menu/7"]
createHashRouter: the route lives after the
import { createRoot } from "react-dom/client";
import { createHashRouter } from "react-router";
import { RouterProvider } from "react-router/dom";
import { routes } from "./routes";
const router = createHashRouter(routes); // same route objects as before
createRoot(document.getElementById("root")).render(<RouterProvider router={router} />);URLs now look like example.com/#/menu/7. The part after # is the URL's fragment, and browsers never send it to the server. So the server only ever sees /, returns index.html, and the router reads /menu/7 from the hash. Refreshing never 404s, and no rewrite rule is needed.
Your <Link to="/menu/7"> stays exactly the same. The router writes href="#/menu/7" for you.
Use it when you can't configure the server: some static hosts, an old company intranet server, or an app opened straight from the file system.
The costs:
- Uglier URLs, and the
#can't be used for normal "jump to section" anchors. - The server never sees the real path, so there's no server rendering, and link previews and search engines only see your home page.
createMemoryRouter: the route lives in memory
A memory router keeps its history in a JavaScript array. The address bar never changes, and it doesn't need a browser URL at all. You choose where it starts:
const router = createMemoryRouter(routes, {
initialEntries: ["/", "/menu/7"], // the history stack
initialIndex: 1, // start on "/menu/7"
});Its main job is testing. You can render any page of your app, starting at any URL, without touching the test environment's location:
import { render, screen } from "@testing-library/react";
import { createMemoryRouter } from "react-router";
import { RouterProvider } from "react-router/dom";
import { routes } from "./routes";
test("the about page has a heading", async () => {
const router = createMemoryRouter(routes, { initialEntries: ["/about"] });
render(<RouterProvider router={router} />);
expect(await screen.findByRole("heading", { name: /about/i })).toBeInTheDocument();
});It also fits embedded widgets (a booking widget inside someone else's page shouldn't change their URL) and tools like Storybook.
To share routes between your app and your tests, export the route array from its own file (routes.jsx) and pass it to whichever router you need.
Which router when?
| Router | Location lives in | Use for |
|---|---|---|
createBrowserRouter | The real path, /menu/7 | Almost every web app. Set up the host's rewrite. |
createHashRouter | The hash, /#/menu/7 | Hosting where you can't add a rewrite |
createMemoryRouter | A JS array | Tests, Storybook, embedded widgets |
Gotchas
Recall
Can you answer these?
0/4
Answer out loud (or in your head) before you tap. That's what makes it stick.
Saved on this device only. No login.
