Define routes and navigation links
Connect a location to a screen
A route is a rule that connects a URL path to a piece of UI. In a single-page application, navigation can change the visible screen without downloading a new HTML document for each internal link. React itself does not provide routing. React Router is a separate dependency. This chapter uses the declarative API documented for React Router 7 (the same core patterns appear in the newer documentation). Import from react-router for this lane; older v6 examples commonly import from react-router-dom. Use a reviewed, patched compatible dependency set with the Chapter 3 client project, not an unreviewed install of an old version. Each snippet is a separate App.jsx; MemoryRouter keeps history inside the app rather than changing the address bar. No package is installed or executed here.
import React from 'react';import { MemoryRouter, Routes, Route, Link } from 'react-router';export default function App() { return <MemoryRouter initialEntries={['/']}> <nav aria-label="Main"><Link to="/">Home</Link>{' | '}<Link to="/trails">Trails</Link></nav> <main><Routes> <Route path="/" element={<h1>Trail guide</h1>} /> <Route path="/trails" element={<h1>Available trails</h1>} /> </Routes></main> </MemoryRouter>;}Initially the heading is Trail guide. Activating Trails should display Available trails while the navigation stays mounted. The router supplies location context, Routes chooses the matching route branch, and element supplies the UI. A Route declaration is not itself a visible menu item. Keep Links and route consumers under a single router; do not wrap an already routed app in a second router.
Show the active destination
import React from 'react';import { MemoryRouter, Routes, Route, NavLink } from 'react-router';export default function App() { return <MemoryRouter initialEntries={['/trails']}> <nav aria-label="Main"> <NavLink to="/" end>Home</NavLink>{' | '} <NavLink to="/trails" style={({isActive}) => ({fontWeight: isActive ? 'bold' : 'normal'})}>Trails</NavLink> </nav> <main><Routes><Route path="/" element={<h1>Trail guide</h1>} /> <Route path="/trails" element={<h1>Available trails</h1>} /></Routes></main> </MemoryRouter>;}Trails should be bold at the initial location. NavLink supplies active state and aria-current="page" when active. The end option makes matching stop at the destination instead of also including deeper paths (the root destination has special matching behavior). Links represent destinations and support normal keyboard and link semantics. Buttons represent actions. Do not replace every link with a click handler on a div.
Navigate after an action
import React, { useState } from 'react';import { MemoryRouter, Routes, Route, Link, useNavigate } from 'react-router';function Checklist() { const [ready, setReady] = useState(false); const navigate = useNavigate(); return <main><h1>Before your walk</h1> <label><input type="checkbox" checked={ready} onChange={e => setReady(e.target.checked)} /> Map packed</label> <button disabled={!ready} onClick={() => navigate('/done')}>Finish checklist</button> </main>;}export default function App() { return <MemoryRouter><Routes> <Route path="/" element={<Checklist />} /> <Route path="/done" element={<main><h1>Checklist complete</h1><Link to="/">Start again</Link></main>} /> </Routes></MemoryRouter>;}Checking Map packed enables the button; activation should navigate to the completion screen. This is a local exercise, not a persisted completion record. useNavigate is a Hook used inside router context, not a function to call during render. Normal navigation adds a history entry; replace: true substitutes the current entry. A numeric navigation such as navigate(-1) depends on the existing history and is not a guaranteed home link. For a real browser address bar and back/forward navigation use BrowserRouter instead of MemoryRouter; direct requests to paths need host support, covered in lesson 3.
Practice
Add a Safety page with a Link and a matching Route. Predict the visible heading and active navigation on each screen. Verify keyboard activation and the initial entry in a real router harness.