React Router Cheatsheet - Routing
This reference is for React developers wiring up client-side navigation with React Router v6. It covers the declarative Routes/Route tree, nested layouts with Outlet, Link/NavLink and programmatic useNavigate, reading dynamic params and refreshing the query string, guarding protected pages, and the reactive data APIs (loader/action) added in v6.4. Each entry fills a concrete gap, like rendering a 404, keeping nav active state, or redirecting after login. After reading you should be able to declare a route tree, link between pages, handle :id params and ?query, and protect authenticated screens.
Router Setup 5
<BrowserRouter><App /></BrowserRouter><Routes><Route path="/" element={<Home />} /></Routes><Route path="/users/:id" element={<User />} /><Route path="*" element={<NotFound />} /><Route path="/" element={<Layout />}>...</Route>Route Params 5
<Route path="/users/:id" element={<User />} />const { id } = useParams()<Route path="/files/*" element={<Files />} />const splat = useParams()["*"]navigate(-1) / navigate(1)Query Params 5
const [params, setParams] = useSearchParams()params.get("q")setParams({ q: "keyword", page: 1 })<Link to="/search?q=react">Search</Link>params.toString()Nested & Layout 5
<Route path="/dashboard" element={<Dashboard />}><Route index element={<Overview />} /><Route path="settings" element={<Settings />} /><Outlet /><Outlet context={{ user }} />Guards & Data 5
function ProtectedRoute({ children }) { return isAuth ? children : <Navigate to="/login" /> }<Route element={<ProtectedRoute><Outlet /></ProtectedRoute>}><Route loader={async ({ params }) => fetchUser(params.id)} /><Route action={async ({ request }) => submitForm(request)} /><Route errorElement={<ErrorBoundary />} />Advanced 6
const location = useLocation()location.pathname / location.search / location.hashconst match = useMatch("/users/:id")const Page = lazy(() => import("./Page"))<Route path="about" element={<Suspense><About /></Suspense>} /><ScrollRestoration />Tips
- React Router v6 replaces v5's <Switch> with <Routes>.
- useNavigate replaces v5's useHistory: navigate(-1) goes back, navigate(1) goes forward.
- Nested routes need <Outlet /> in the parent to render children; an index route matches the parent path itself.
- NavLink's className accepts an { isActive } callback for active styling.
- Get dynamic params :id with useParams(); read query params ?q= with useSearchParams().
Official References
Each command links to its official documentation below, so you can verify the latest usage and read deeper.
Maintained by LaoHand
Publicly updated on Jul 21, 2026, continuously proofread against official docs.
Contact Us
Wrong command or description? Send us corrections, business inquiries or product feedback by email.
Contact Us