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.

How to Read This

Every v8 API is tagged with the modes it works in. F is Framework, D is Data, C is Declarative. Everything imports from react-router unless a row says otherwise, and route config helpers come from @react-router/dev/routes.

Components

ComponentWhat it doesModes
LinkClient side anchor, plus prefetch and discoverF D C
NavLinkLink that knows isActive and isPendingF D C
FormProgressively enhanced form that posts to an actionF D
OutletRenders the matched child routeF D C
NavigateNavigates on renderF D C
AwaitRenders a streamed promise inside SuspenseF D
ScrollRestorationRestores scroll position per history entryF D
MetaRenders collected meta output into the headF
LinksRenders collected links output plus route CSSF
ScriptsThe hydration script, required for client routingF
PrefetchPageLinksPrefetch a route's assets imperativelyF
Routes / RouteJSX route declarationC

Routers

APIImport fromModes
HydratedRouterreact-router/domF
ServerRouterreact-routerF
createBrowserRouterreact-routerD
createHashRouterreact-routerD
createMemoryRouterreact-routerD
createStaticRouter / createStaticHandlerreact-routerD
RouterProviderreact-router/domD
StaticRouterProviderreact-routerD
BrowserRouter / HashRouter / MemoryRouterreact-routerC
StaticRouter / HistoryRouter / Routerreact-routerC

Framework mode wires its own routers through the Vite plugin, so you never call one of these unless you are writing a custom entry file.

Data Hooks

HookReturnsModes
useLoaderDataThis route's loader dataF D
useRouteLoaderData(id)Another matched route's loader dataF D
useActionDataThis route's action return valueF D
useFetcherAn independent loader and action callerF D
useFetchersEvery in-flight fetcherF D
useSubmitSubmit programmaticallyF D
useFormActionThe resolved action URL for this routeF D
useNavigationGlobal pending state and in-flight form dataF D
useRevalidatorRe-run every loader on the pageF D
useMatchesEvery match, with id, params, loaderData, handleF D
useRouteErrorThe error inside an error boundaryF D
useAsyncValueThe resolved value inside AwaitF D
useAsyncErrorThe rejection inside AwaitF D

In a Framework mode route module prefer the typed props (loaderData, actionData, params, matches) over these hooks. The hooks are for components nested below the route.

URL and Navigation Hooks

HookReturnsModes
useNavigateImperative navigate functionF D C
useLocationThe current LocationF D C
useParamsMatched dynamic segments, all stringsF D C
useSearchParams[URLSearchParams, setter]F D C
useHref(to)The resolved href, basename includedF D C
useResolvedPath(to)What a relative to resolves toF D C
useMatch(pattern)A match object or nullF D C
useNavigationTypePOP, PUSH, or REPLACEF D C
useOutletThe child route elementF D C
useOutletContextValue passed via <Outlet context>F D C
useRoutesBuild routes from objects, no JSXF D C
useInRouterContextWhether a router is above youF D C
useBeforeUnloadRun something before the tab closesF D C
useLinkClickHandlerBuild a custom link componentF D C
useBlockerBlock a client side navigationF D
useViewTransitionState(to)Whether a View Transition is activeF D
unstable_usePromptConfirm dialog before leavingF D
unstable_useRouterStateactive and pending state in one hookF D

Utilities

UtilityDoesModes
redirect(url, init?)Redirect from a loader or actionF D
redirectDocument(url)Redirect with a full document loadF D
replace(url)Redirect that replaces the history entryF D
data(value, init)Return or throw a value with status and headersF D
isRouteErrorResponse(e)Narrow an error to a route error responseF D
createContext<T>()Create a typed middleware and loader contextF D
RouterContextProviderThe context object handed to handlersF D
createRoutesStub(routes)Test router context for unit testsF D
createRoutesFromElementsTurn JSX routes into route objectsD
createCookie(name, opts)A signed, typed cookieF D
createCookieSessionStorageSession stored in the cookie itselfF D
createMemorySessionStorageSession in memory, dev and testsF D
href(path, params)Type safe URL from the route configF
generatePath(pattern, params)Fill params into a patternF D C
matchPath(pattern, pathname)Match one pattern, or nullF D C
matchRoutes(routes, location)The matched branchF D C
resolvePath(to, from)Resolve one path against anotherF D C
createPath / parsePathPath object to string and backF D C
createSearchParams(init)Build a URLSearchParamsF D C
renderMatches(matches)Render a matched branch by handF D C

Route Config Helpers

From @react-router/dev/routes, used only in app/routes.ts.

HelperSignature
routeroute(path, file, children?)
indexindex(file)
layoutlayout(file, children)
prefixprefix(path, children)
relativerelative(dir), helpers with file paths relative to dir
RouteConfigThe type to satisfies on the default export
flatRoutesFrom @react-router/fs-routes, file based conventions