Skip to content
Tutorials Center
My learning

Topics

Lesson 123 of 132 ยท Projects

Project: Blog App with Routing

Build a small blog in React with a hash router you write yourself, route params, a reducer-backed CRUD flow, validated forms, tags and query-string filters.

A blog is the classic multi-page app: a list page, a page per post, a form to write posts, and links between them. It forces you to answer questions that single-screen apps never raise. Where does the current page live? How does a post page know which post to show? What happens when someone opens a link to a post that does not exist?

In this project you will build a blog with create, read, update and delete (CRUD) in three stages, using a tiny router built from the ideas in client-side routing. The preview sandbox has no real URLs, so the router uses the hash (#/posts/2), exactly as that lesson does. The structure is the same as with React Router, so you can swap it later. Posts live in memory, so they reset when the preview reloads.

The plan

The URL is the source of truth for "which page am I on", so the page is derived from the location, not stored in state.

Path Page
#/ list of posts (optionally ?tag=react)
#/posts/new form for a new post
#/posts/:id one post
#/posts/:id/edit edit form
anything else "page not found"

The data (the posts) is real state and is needed by several pages, so it will live in a reducer behind a context (context, reducers).

Stage 1: a router and read-only pages

Three small pieces make a router: a hook that reads the hash with useSyncExternalStore, a matchPath function that turns /posts/:id into params, and a Link that is just an anchor. A missing post is an ordinary case, so the post page handles it itself.

Stage 1: routing and read-only pages

Notice that the page is picked with plain if statements during render, and that PostPage converts the string param with Number(id): route params are always strings. The two links at the top show the two kinds of "not found": a valid route with an unknown id, and a URL that matches no route.

Stage 2: create, edit and delete

Now the posts become state. The reducer has four actions, and each one returns a new array without mutating the old one (objects and arrays in state). Pages read posts with a usePosts() hook so no component needs to know where they come from.

The form is one component used for both "new" and "edit". It keeps the draft text in its own state, validates on submit, and calls onSave(values). The page decides what saving means, and then navigates by changing the hash. Delete is a two-step button, because window.confirm is blocked in sandboxed previews.

Stage 2: a CRUD blog

Three decisions deserve a closer look:

  1. Route order matters. /posts/new is checked before /posts/:id, otherwise new would be treated as an id. Every router, including React Router's ranking, has to solve this.
  2. Saving navigates. After dispatch, the handler calls navigate(...). Navigation is an event consequence, so it belongs in the handler, not in an effect.
  3. The form owns its draft. Typing does not touch the global state, so the list does not re-render on every keystroke. Only a successful save reaches the reducer. The form copies the post once through initialValues, which is why the edit route re-mounts it when you move between posts. See forms and inputs and form validation.

Stage 3: tags and a query-string filter

Filters like "show only posts tagged react" should be shareable, so they belong in the URL as a query string: #/?tag=react. The hook already parses the query with URLSearchParams. The list reads query.get('tag'), filters during render, and tag chips are plain links. No filter state is stored anywhere. Add a tags field to the data and a comma-separated tags input in the form, and the blog is finished.

Stage 3: tags and a filter in the URL

The filter is a pure function of the URL. Reload with ?tag=react in the address and the same list appears: that is the reason to put view state in the URL rather than in useState. parseTags trims, lowercases and removes duplicates with a Set, which is what the form should call before saving.

Ideas to extend it

  • Combine the stages: add the tags input to PostForm and filter the context-backed list.
  • Add a search box that writes ?q= into the hash as the user types (debounce it).
  • Replace the home-made router with React Router: the pages keep the same shape.
  • Load posts from an API with data fetching and show loading and error states.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why does the route for /posts/new come before /posts/:id?

    A dynamic segment matches any value, so more specific patterns must be tried first. Real routers rank patterns for you; our simple one relies on order.
  2. 2. What type is a route param such as params.id?

    URLs are text. Convert with Number(id) before comparing with numeric ids in your data.
  3. 3. Where should an active filter such as tag=react be stored?

    View state that people may want to link to or reload belongs in the URL. The list derives what to show from the query during render.
  4. 4. After a successful save, where should the app navigate?

    Navigation is a consequence of a user event, so it belongs in the event handler. Effects are for synchronising with outside systems, and reducers must stay pure.

Summary

  • The location is the source of truth for the current page; derive the page from it during render.
  • A router is a location hook (useSyncExternalStore), a pattern matcher and a Link; params arrive as strings.
  • Put shared data in a reducer behind a context, and expose it with a custom hook.
  • One form component can serve "new" and "edit"; it owns its draft and validates on submit.
  • Navigate from event handlers, handle unknown ids and URLs explicitly, and keep filters in the query string.

Next: Project: Color Palette Generator