AI Hints
Home›React Tutorial›Routing
React · Lesson 69 of 120

Nested Routes

Shared layouts.

⏱ 2 min read▶ Video at 57:30🎓 Free
Watch this lesson in the React courseWatch on YouTube ↗Full React course

Most pages share a header and a footer. Nested routes let a parent route draw that layout once, with a slot where each child page appears.

  • Shared header
  • Draw it once
  • A slot for pages

A shared layout

Layout holds the header and footer, shared by every page. Outlet marks where the child page appears. The routes inside the layout route are its children. Index is the page for the parent's own path. We click shop. Run it. The header stays, and only the middle changes.

App.jsxjsx
import { MemoryRouter, Routes, Route, Link, Outlet } from "react-router";
function Layout() {
  return (<>
    <header className="box"><b>Northwind Bikes</b> <Link to="/">Home</Link> <Link id="shop" to="/shop">Shop</Link></header>
    <main><Outlet /></main>
    <footer>© Northwind Bikes</footer>
  </>);
}
export default function App() {
  return (
    <MemoryRouter><Routes>
      <Route element={<Layout />}>
        <Route index element={<h2>Home</h2>} />
        <Route path="shop" element={<h2>Shop: 5 products</h2>} />
      </Route>
    </Routes></MemoryRouter>
  );
}
Also uses style.css
body{font-family:-apple-system,Helvetica,Arial,sans-serif;margin:22px;font-size:21px;color:#1d1d1f;background:#fafafa}
h1{font-size:34px;margin:0 0 14px} h2{font-size:26px;margin:0 0 10px}
button{font-size:19px;padding:9px 18px;border-radius:9px;border:0;background:#61dafb;color:#111;font-weight:600;margin:4px 6px 4px 0}
input{font-size:19px;padding:8px 10px;border:2px solid #bbb;border-radius:8px}
li{margin:5px 0} .box{padding:14px;border-radius:12px;background:#fff;border:2px solid #ddd;margin:8px 0}
.done{text-decoration:line-through;color:#999} small{color:crimson;display:block;font-size:16px} .big{font-size:64px;font-weight:800}
Also uses actions.json
[{"click": "#shop"}]

Summary

A layout route draws shared parts once, and Outlet shows the child page. Next, a new section on concurrent features.

Key takeaways
  • Layout route
  • <Outlet />
  • index
Next up: useTransition — stay responsive