JavaScript Curriculum
React Router
mediumThe coffee shop app needs a home page, a menu page, individual item detail pages at /menu/:id, and a cart. Each page is a React component — React Router wires URL paths to the right component.
React Router
React Router is the standard routing library for React. It maps URL paths to components, handles navigation without page reloads, and provides hooks to read URL parameters and navigate programmatically.
Setup, Routes, Link, useParams, useNavigate
The four core concepts — all in one place:
BrowserRouter wraps the app. Routes renders the first matching Route. Each Route maps a path to a component.
// main.jsx — wrap the app import { BrowserRouter } from 'react-router-dom' ReactDOM.createRoot(document.getElementById('root')).render( <BrowserRouter> <App /> </BrowserRouter> ) // App.jsx — define routes import { Routes, Route } from 'react-router-dom' function App() { return ( <Routes> <Route path="/" element={<Home />} /> <Route path="/menu" element={<Menu />} /> <Route path="/menu/:id" element={<ItemDetail />} /> <Route path="/cart" element={<Cart />} /> <Route path="*" element={<NotFound />} /> </Routes> ) }
Navigation Patterns — With Browser History
Simulate navigation, history stack, and useNavigate patterns:
// Programmatic navigation patterns: // After form submit → go to confirmation: const navigate = useNavigate() const handleSubmit = async (data) => { const order = await submitOrder(data) navigate(`/orders/${order.id}`) } // Protected route — redirect if not logged in: function PrivateRoute({ children }) { const { user } = useAuth() if (!user) { return <Navigate to="/login" replace /> } return children } // Go back after action: const navigate = useNavigate() <button onClick={() => navigate(-1)}>← Back</button> // Replace entry (login → dashboard, no back to login): navigate('/dashboard', { replace: true })
Nested Routes — Layout + Outlet
Share a layout across multiple routes using nesting:
Your Challenge
Build a mini coffee shop router: BrowserRouter in main.jsx. Layout route at / with Header, <Outlet />, Footer. Child routes: index→Home, menu→MenuList, menu/:id→ItemDetail, cart→Cart, *→NotFound. In ItemDetail: useParams() to get id, find item, show name and price. NavLink for active styles.
Challenge
Set up BrowserRouter. Define routes: / → Home, /menu → MenuList, /menu/:id → ItemDetail, /cart → Cart, * → NotFound. Add a nav with NavLink. In ItemDetail, read the id param and find the matching item. Add a 'Back to menu' link.
React Router
mediumThe coffee shop app needs a home page, a menu page, individual item detail pages at /menu/:id, and a cart. Each page is a React component — React Router wires URL paths to the right component.
React Router
React Router is the standard routing library for React. It maps URL paths to components, handles navigation without page reloads, and provides hooks to read URL parameters and navigate programmatically.
Setup, Routes, Link, useParams, useNavigate
The four core concepts — all in one place:
BrowserRouter wraps the app. Routes renders the first matching Route. Each Route maps a path to a component.
// main.jsx — wrap the app import { BrowserRouter } from 'react-router-dom' ReactDOM.createRoot(document.getElementById('root')).render( <BrowserRouter> <App /> </BrowserRouter> ) // App.jsx — define routes import { Routes, Route } from 'react-router-dom' function App() { return ( <Routes> <Route path="/" element={<Home />} /> <Route path="/menu" element={<Menu />} /> <Route path="/menu/:id" element={<ItemDetail />} /> <Route path="/cart" element={<Cart />} /> <Route path="*" element={<NotFound />} /> </Routes> ) }
Navigation Patterns — With Browser History
Simulate navigation, history stack, and useNavigate patterns:
// Programmatic navigation patterns: // After form submit → go to confirmation: const navigate = useNavigate() const handleSubmit = async (data) => { const order = await submitOrder(data) navigate(`/orders/${order.id}`) } // Protected route — redirect if not logged in: function PrivateRoute({ children }) { const { user } = useAuth() if (!user) { return <Navigate to="/login" replace /> } return children } // Go back after action: const navigate = useNavigate() <button onClick={() => navigate(-1)}>← Back</button> // Replace entry (login → dashboard, no back to login): navigate('/dashboard', { replace: true })
Nested Routes — Layout + Outlet
Share a layout across multiple routes using nesting:
Your Challenge
Build a mini coffee shop router: BrowserRouter in main.jsx. Layout route at / with Header, <Outlet />, Footer. Child routes: index→Home, menu→MenuList, menu/:id→ItemDetail, cart→Cart, *→NotFound. In ItemDetail: useParams() to get id, find item, show name and price. NavLink for active styles.
Challenge
Set up BrowserRouter. Define routes: / → Home, /menu → MenuList, /menu/:id → ItemDetail, /cart → Cart, * → NotFound. Add a nav with NavLink. In ItemDetail, read the id param and find the matching item. Add a 'Back to menu' link.