Simple Blog with Routing
What you'll build
A blog with a home page listing all posts, and a post detail page for each post. Uses React Router for navigation between the list and detail views with dynamic URL params.
Goals
- Set up React Router with routes
- Navigate between pages with Link
- Use dynamic route params (useParams)
- Render different pages conditionally
Prerequisites
- React Router basics
- Components and props
- List rendering
Steps
- 1
Define blog post data
Create an array of blog post objects with id, title, and content.
Codeconst posts = [ { id: 1, title: "Getting Started with React", content: "React is..." }, { id: 2, title: "Understanding Hooks", content: "Hooks let you..." }, { id: 3, title: "State Management", content: "There are many ways..." }, ];Expected result
Blog data is ready.
- 2
Create the Home and PostDetail components
Home lists all posts as links. PostDetail reads the :id param with useParams and shows the matching post.
Codefunction Home() { return ( <div> <h1>Blog</h1> {posts.map(p => ( <div key={p.id}> <Link to={`/post/${p.id}`}>{p.title}</Link> </div> ))} </div> ); } function PostDetail() { const { id } = useParams(); const post = posts.find(p => p.id === parseInt(id)); return ( <div> <Link to="/">Back</Link> <h1>{post.title}</h1> <p>{post.content}</p> </div> ); }Expected result
Home shows post links. PostDetail shows a single post.
- 3
Set up the router
Wrap the app in BrowserRouter and define routes for Home and PostDetail.
Code<BrowserRouter> <Routes> <Route path="/" element={<Home />} /> <Route path="/post/:id" element={<PostDetail />} /> </Routes> </BrowserRouter>Expected result
Clicking a post link navigates to its detail page. Back returns to the list.
- 4
Add state management & event handling
Use useState to manage component state and add onClick/onChange handlers to interactive elements. State is the heart of React components, and event handling lets them respond to user actions.
Codeimport { useState } from "react"; export function MyComponent() { const [count, setCount] = useState(0); const [text, setText] = useState(""); return ( <div> <button onClick={() => setCount(c => c + 1)}> Count: {count} </button> <input value={text} onChange={(e) => setText(e.target.value)} /> </div> ); }Expected result
Clicking the button increments the count; the input content updates in real time as you type.
- 5
Add Props validation & component split
Define a Props interface (TypeScript) or propTypes for the component, and split large components into smaller sub-components. Type-safe Props make the component interface clear and reusable.
Codeinterface ChildProps { label: string; value: number; onChange: (v: number) => void; } function Child({ label, value, onChange }: ChildProps) { return ( <label> {label} <input type="number" value={value} onChange={(e) => onChange(Number(e.target.value))} /> </label> ); }Expected result
The child component receives type-safe Props; the parent can reuse it to render multiple fields.
Try it yourself
What you learned
You learned to build a multi-page app with React Router: defining routes, navigating with Link, reading dynamic URL params with useParams, and structuring an app with multiple views. This is the foundation of any React SPA with navigation.
Related Resources
Related Lessons
- React Router
Routing and navigation.
- Components
Page components.
Look up unfamiliar terms
A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.