React Router Cheatsheet

API Reference

Use this React Router reference while you build software engineering projects, review code, or refresh the syntax you reach for most.

Hooks Reference

HookReturnsMode
useNavigateA function to navigate imperativelyAll
useLocationThe current LocationAll
useParamsThe matched dynamic segmentsAll
useSearchParams[URLSearchParams, setter]All
useMatch(pattern)A match object or nullAll
useMatchesEvery matched route in the branchData
useHref(to)The resolved href, with basenameAll
useResolvedPath(to)What a relative to resolves toAll
useInRouterContextWhether a Router is above youAll
useNavigationTypePOP, PUSH, or REPLACEAll
useOutletThe child route elementAll
useOutletContextContext passed via <Outlet context>All
useRoutesBuild routes from an object, no JSXAll
useLoaderDataThis route's loader dataData
useRouteLoaderData(id)Another route's loader dataData
useActionDataThis route's action return valueData
useNavigationGlobal pending stateData
useFetcherAn independent loader/action callerData
useFetchersEvery in-flight fetcherData
useSubmitSubmit programmaticallyData
useFormActionThe resolved action URL for this routeData
useRevalidatorManually re-run loadersData
useRouteErrorThe error in an error boundaryData
useBlockerBlock a client-side navigationData
useBeforeUnloadRun something before the tab closesData
useViewTransitionState(to)Whether a View Transition to to is activeData

Components Reference

ComponentPurpose
<BrowserRouter> etc.Provide a router (declarative mode)
<RouterProvider router>Provide a data router
<Routes> / <Route>Declare routes as JSX
<Outlet>Render the matched child route
<Link>A client-side anchor
<NavLink>A Link that knows if it is active
<Navigate>Navigate on render
<Form>A form that posts to a route action
<Await>Render a streamed promise
<ScrollRestoration>Restore scroll position per history entry
<Meta> / <Links> / <Scripts>Framework mode document tags
<ScrollRestoration> in rootFramework mode root template

Outlet Context

Pass values from a layout to whichever child is rendered, without a separate context provider.

function DashboardLayout() {
  const [tab, setTab] = useState("all");
  return <Outlet context={{ tab, setTab }} />;
}

function Child() {
  const { tab, setTab } = useOutletContext();
}
// Typed
type DashContext = { tab: string; setTab: (t: string) => void };
const { tab } = useOutletContext<DashContext>();

Prefer a root loader plus useRouteLoaderData for server data, and useOutletContext for client-only UI state a layout owns.

Basename

createBrowserRouter(routes, { basename: "/app" });
<BrowserRouter basename="/app">
// react-router.config.ts, framework mode
export default { basename: "/app" } satisfies Config;

With a basename, <Link to="/about"> produces /app/about. Do not include the basename in your to props, or you get /app/app/about.