{"id":77,"date":"2026-07-04T10:10:59","date_gmt":"2026-07-04T10:10:59","guid":{"rendered":"https:\/\/www.kindsonthegenius.com\/react\/07-react-router\/"},"modified":"2026-07-04T10:14:13","modified_gmt":"2026-07-04T10:14:13","slug":"07-react-router","status":"publish","type":"post","link":"https:\/\/www.kindsonthegenius.com\/react\/07-react-router\/","title":{"rendered":"React Router \u2014 Multi-Page Navigation (React 19+)"},"content":{"rendered":"<p><!-- ktg-updated-banner --><\/p>\n<div class=\"ktg-updated-banner\" style=\"margin:0 0 1.25em;padding:0.75em 1em;background:#fefce8;border-left:4px solid #ca8a04;border-radius:4px;\">\n<p><strong>Updated July 4, 2026.<\/strong> Refreshed for React 19 and current SEO best practices.<\/p>\n<\/div>\n<p><!-- ktg-series-toc --><\/p>\n<nav class=\"ktg-series-toc\" aria-label=\"React tutorial table of contents\" style=\"margin:1.5em 0;padding:1em 1.25em;background:#f8fafc;border:1px solid #e2e8f0;border-radius:8px;\">\n<h2>Table of Contents<\/h2>\n<p>Follow the React tutorial series in order:<\/p>\n<ol>\n<li><a href=\"https:\/\/www.kindsonthegenius.com\/react\/01-react-introduction\/\">Lesson 1: React Tutorial \u2014 Introduction (2026)<\/a><\/li>\n<li><a href=\"https:\/\/www.kindsonthegenius.com\/react\/02-react-setup\/\">Lesson 2: React Setup \u2014 Create Your First App with Vite<\/a><\/li>\n<li><a href=\"https:\/\/www.kindsonthegenius.com\/react\/03-react-jsx-and-components\/\">Lesson 3: React JSX and Components \u2014 Build Your First UI<\/a><\/li>\n<li><a href=\"https:\/\/www.kindsonthegenius.com\/react\/04-react-state-usestate\/\">Lesson 4: React State and useState \u2014 Interactive UI (React 19+)<\/a><\/li>\n<li><a href=\"https:\/\/www.kindsonthegenius.com\/react\/05-react-useeffect\/\">Lesson 5: React useEffect \u2014 Side Effects and Data Fetching (React 19+)<\/a><\/li>\n<li><a href=\"https:\/\/www.kindsonthegenius.com\/react\/06-react-forms\/\">Lesson 6: React Forms \u2014 Controlled Inputs and Validation (React 19+)<\/a><\/li>\n<li><strong aria-current=\"page\">Lesson 7: React Router \u2014 Multi-Page Navigation (React 19+)<\/strong><\/li>\n<li><a href=\"https:\/\/www.kindsonthegenius.com\/react\/\">\u2190 Back to React Tutorial hub<\/a><\/li>\n<\/ol>\n<\/nav>\n<p><!-- ktg-react-modern --><\/p>\n<div class=\"ktg-react-modern\" style=\"margin:1.5em 0;padding:1em;background:#eff6ff;border-left:4px solid #2563eb;border-radius:4px;\">\n<h4>React 19 Notes<\/h4>\n<p>React Router v6 uses a declarative <code>&lt;Routes&gt;<\/code> API with nested <code>&lt;Outlet \/&gt;<\/code> layouts. For SEO-critical pages, pair React skills with <strong>Next.js<\/strong> or prerender key routes on your static host.<\/p>\n<\/div>\n<p>This lesson introduces <strong>React Router<\/strong> \u2014 the standard library for client-side routing in React SPAs. You will install <code>react-router-dom<\/code>, define routes, navigate with <code>Link<\/code>, read URL parameters, and structure a small multi-page app.<\/p>\n<p><strong>Prerequisites:<\/strong> Lessons 1\u20136 with a Vite React project. <strong>Estimated time:<\/strong> 50\u201360 minutes.<\/p>\n<h4 id=\"why-router\">1. Why Client-Side Routing?<\/h4>\n<p>Without a router, a React app is a single view. Users expect URLs like <code>\/about<\/code> and <code>\/users\/42<\/code> that work with bookmarks and the back button. React Router maps URLs to components without full page reloads.<\/p>\n<figure style=\"margin:1.5em 0;text-align:center;\"><img decoding=\"async\" src=\"https:\/\/www.kindsonthegenius.com\/react\/wp-content\/uploads\/2026\/07\/07-react-router-section-2.jpg\" alt=\"React single-page application routing\" width=\"1200\" height=\"675\" loading=\"lazy\" style=\"max-width:100%;height:auto;border-radius:8px;border:1px solid #dee2e6;\" \/><figcaption style=\"font-size:0.85em;color:#666;margin-top:0.5em;\">Photo by <a href=\"https:\/\/unsplash.com\/@afgprogrammer?utm_source=kindsonthegenius&#038;utm_medium=referral\" rel=\"noopener noreferrer\" target=\"_blank\">Mohammad Rahmani<\/a> on <a href=\"https:\/\/unsplash.com\/?utm_source=kindsonthegenius&#038;utm_medium=referral\" rel=\"noopener noreferrer\" target=\"_blank\">Unsplash<\/a><\/figcaption><\/figure>\n<h4 id=\"install\">2. Install react-router-dom<\/h4>\n<pre><code class=\"language-bash\">npm install react-router-dom\n<\/code><\/pre>\n<p>React Router v6+ is the current API. It works with Vite and React 19 out of the box.<\/p>\n<h4 id=\"basic-setup\">3. Basic Route Setup<\/h4>\n<p>Wrap your app in <code>BrowserRouter<\/code> and define routes in <code>main.jsx<\/code> or <code>App.jsx<\/code>:<\/p>\n<pre><code class=\"language-jsx\">import { BrowserRouter, Routes, Route } from 'react-router-dom';\nimport Home from '.\/pages\/Home';\nimport About from '.\/pages\/About';\nimport NotFound from '.\/pages\/NotFound';\n\nfunction App() {\n  return (\n    &lt;BrowserRouter&gt;\n      &lt;Routes&gt;\n        &lt;Route path=\"\/\" element={&lt;Home \/&gt;} \/&gt;\n        &lt;Route path=\"\/about\" element={&lt;About \/&gt;} \/&gt;\n        &lt;Route path=\"*\" element={&lt;NotFound \/&gt;} \/&gt;\n      &lt;\/Routes&gt;\n    &lt;\/BrowserRouter&gt;\n  );\n}\n\nexport default App;\n<\/code><\/pre>\n<p>Create simple page components under <code>src\/pages\/<\/code> \u2014 each returns a heading and paragraph for now.<\/p>\n<h4 id=\"link-navlink\">4. Link and NavLink<\/h4>\n<pre><code class=\"language-jsx\">import { Link, NavLink } from 'react-router-dom';\n\nfunction Navbar() {\n  return (\n    &lt;nav&gt;\n      &lt;Link to=\"\/\"&gt;Home&lt;\/Link&gt;\n      &lt;NavLink\n        to=\"\/about\"\n        style={({ isActive }) =&gt; ({ fontWeight: isActive ? 'bold' : 'normal' })}\n      &gt;\n        About\n      &lt;\/NavLink&gt;\n    &lt;\/nav&gt;\n  );\n}\n<\/code><\/pre>\n<p>Use <code>Link<\/code> instead of <code>&lt;a href&gt;<\/code> for internal routes \u2014 it prevents full reloads. <code>NavLink<\/code> adds an <code>isActive<\/code> callback for styling the current page.<\/p>\n<h4 id=\"url-params\">5. URL Parameters<\/h4>\n<pre><code class=\"language-jsx\">\/\/ Route definition\n&lt;Route path=\"\/lessons\/:slug\" element={&lt;LessonDetail \/&gt;} \/&gt;\n\n\/\/ LessonDetail.jsx\nimport { useParams } from 'react-router-dom';\n\nfunction LessonDetail() {\n  const { slug } = useParams();\n  return &lt;h1&gt;Lesson: {slug}&lt;\/h1&gt;;\n}\n<\/code><\/pre>\n<p>Dynamic segments (<code>:slug<\/code>) are common for blog posts, product pages, and user profiles \u2014 core patterns in a <strong>react router tutorial<\/strong>.<\/p>\n<h4 id=\"layout-routes\">6. Layout Routes with Outlet<\/h4>\n<pre><code class=\"language-jsx\">function Layout() {\n  return (\n    &lt;&gt;\n      &lt;Navbar \/&gt;\n      &lt;main&gt;\n        &lt;Outlet \/&gt;\n      &lt;\/main&gt;\n    &lt;\/&gt;\n  );\n}\n\n\/\/ Routes\n&lt;Route element={&lt;Layout \/&gt;}&gt;\n  &lt;Route path=\"\/\" element={&lt;Home \/&gt;} \/&gt;\n  &lt;Route path=\"\/about\" element={&lt;About \/&gt;} \/&gt;\n&lt;\/Route&gt;\n<\/code><\/pre>\n<p><code>Outlet<\/code> renders the matched child route \u2014 shared nav and footer without duplicating markup.<\/p>\n<h4 id=\"programmatic-nav\">7. Programmatic Navigation<\/h4>\n<pre><code class=\"language-jsx\">import { useNavigate } from 'react-router-dom';\n\nfunction LoginForm() {\n  const navigate = useNavigate();\n\n  function onSuccess() {\n    navigate('\/dashboard', { replace: true });\n  }\n}\n<\/code><\/pre>\n<p>Use <code>navigate<\/code> after form submit or logout. <code>replace: true<\/code> avoids stacking history entries.<\/p>\n<h4 id=\"vite-base\">8. Vite and Production Deploy<\/h4>\n<p>For SPAs on static hosts, configure the server to fallback to <code>index.html<\/code> for unknown paths \u2014 otherwise refreshing <code>\/about<\/code> returns 404. Vite dev server handles this automatically; production depends on your host (Netlify, Vercel, nginx rewrite rules).<\/p>\n<h4 id=\"next\">9. Next Steps<\/h4>\n<p>You now have a seven-lesson foundation: setup, JSX, state, effects, forms, and routing. Explore <a href=\"https:\/\/www.kindsonthegenius.com\/angular\/\">Angular tutorials<\/a> for a full-framework comparison, or join <a href=\"https:\/\/www.alkademy.com\/courses\">Alkademy<\/a> for live React projects.<\/p>\n<p>Series: <a href=\"https:\/\/www.kindsonthegenius.com\/react\/06-react-forms\/\">Lesson 6<\/a> \u00b7 <a href=\"https:\/\/www.kindsonthegenius.com\/react\/\">Tutorial hub<\/a><\/p>\n<h4 id=\"faq\">Frequently Asked Questions<\/h4>\n<p><strong>What is React Router used for?<\/strong><br \/>\nReact Router maps URL paths to React components, enabling multi-page navigation in single-page applications without full browser reloads.<\/p>\n<p><strong>React Router vs Next.js routing?<\/strong><br \/>\nReact Router is client-side only \u2014 ideal for Vite SPAs. Next.js includes file-based routing with server rendering for SEO-heavy sites.<\/p>\n<p><strong>How do I protect routes that require login?<\/strong><br \/>\nCreate a wrapper component that checks auth state and redirects with <code>Navigate to=\"\/login\"<\/code> or <code>useNavigate<\/code> when the user is not authenticated.<\/p>\n<p><!-- ktg-faq-schema --><br \/>\n<script type=\"application\/ld+json\">{\"@context\":\"https:\/\/schema.org\",\"@type\":\"FAQPage\",\"mainEntity\":[{\"@type\":\"Question\",\"name\":\"What is React Router used for?\",\"acceptedAnswer\":{\"@type\":\"Answer\",\"text\":\"React Router maps URL paths to React components, enabling multi-page navigation in single-page applications without full browser reloads.\"}},{\"@type\":\"Question\",\"name\":\"React Router vs Next.js routing?\",\"acceptedAnswer\":{\"@type\":\"Answer\",\"text\":\"React Router is client-side for Vite SPAs. Next.js adds file-based routing and server rendering for SEO-heavy production sites.\"}}]}<\/script><\/p>\n<p><!-- ktg-lesson-nav --><\/p>\n<nav class=\"ktg-lesson-nav\" aria-label=\"Lesson navigation\">\n<p><strong>Previous:<\/strong> <a href=\"https:\/\/www.kindsonthegenius.com\/react\/06-react-forms\/\">Lesson 6: React Forms \u2014 Controlled Inputs and Validation (React 19+)<\/a><\/p>\n<\/nav>\n<p><!-- ktg-alkademy-cta --><\/p>\n<div class=\"ktg-alkademy-cta\">\n<p><strong>Want live React or frontend classes?<\/strong> Join <a href=\"https:\/\/www.alkademy.com\/courses\" target=\"_blank\" rel=\"noopener noreferrer\">Alkademy<\/a> for instructor-led React and JavaScript courses with hands-on projects.<\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Updated July 4, 2026. Refreshed for React 19 and current SEO best practices. Table of Contents Follow the React tutorial series in order: Lesson 1: React Tutorial \u2014 Introduction (2026) Lesson 2: React Setup \u2014 Create Your First App with Vite Lesson 3: React JSX and Components \u2014 Build Your First UI Lesson 4: React [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"pagelayer_contact_templates":[],"_pagelayer_content":"","footnotes":""},"categories":[2],"tags":[],"class_list":["post-77","post","type-post","status-publish","format-standard","hentry","category-react-tutorials"],"_links":{"self":[{"href":"https:\/\/www.kindsonthegenius.com\/react\/wp-json\/wp\/v2\/posts\/77","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.kindsonthegenius.com\/react\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.kindsonthegenius.com\/react\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/react\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/react\/wp-json\/wp\/v2\/comments?post=77"}],"version-history":[{"count":2,"href":"https:\/\/www.kindsonthegenius.com\/react\/wp-json\/wp\/v2\/posts\/77\/revisions"}],"predecessor-version":[{"id":79,"href":"https:\/\/www.kindsonthegenius.com\/react\/wp-json\/wp\/v2\/posts\/77\/revisions\/79"}],"wp:attachment":[{"href":"https:\/\/www.kindsonthegenius.com\/react\/wp-json\/wp\/v2\/media?parent=77"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/react\/wp-json\/wp\/v2\/categories?post=77"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/react\/wp-json\/wp\/v2\/tags?post=77"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}