Skip to content
Tutorials Center
My learning

Topics

Lesson 36 of 39 ยท Projects and Practice

Project: A Private Notes App

Build a small private notes app with Remix and React Router: routes, loaders, actions, validation, sessions, ownership checks and error boundaries, with a runnable model of the whole flow.

This project joins the chapters of this course into one small application: a private notes app. Visitors register, sign in, create, edit and delete their own notes and can never see anyone else's. It uses nested routes, loaders, actions, validation, sessions, ownership checks, error boundaries and a redirect after every mutation. The real React Router code is shown read-only, since the live editor cannot run a framework server. A runnable stand-in in plain JavaScript models the routes, so you can check the logic and the security rules end to end.

Plan the routes

URL Route module Loader Action
/ home.tsx none none
/register register.tsx redirect if signed in create user, start session
/login login.tsx redirect if signed in verify, start session
/logout logout.tsx none destroy session
/notes notes.tsx (layout) require user, list titles none
/notes/new notes.new.tsx none validate, create, redirect
/notes/:id notes.$id.tsx load own note or 404 update or delete
flowchart LR
  Home["/"] --> Login["/login"]
  Login -->|"action ok"| Notes["/notes (layout + list)"]
  Notes --> New["/notes/new"]
  Notes --> One["/notes/:id"]
  New -->|"redirect"| One
  One -->|"delete, redirect"| Notes

The notes layout renders the list beside an Outlet, so choosing a note only reloads the child.

The route config in framework mode:

TypeScript
// app/routes.ts (read-only)
import { type RouteConfig, index, route, layout } from "@react-router/dev/routes";

export default [
  index("routes/home.tsx"),
  route("register", "routes/register.tsx"),
  route("login", "routes/login.tsx"),
  route("logout", "routes/logout.tsx"),
  route("notes", "routes/notes.tsx", [
    index("routes/notes._index.tsx"),
    route("new", "routes/notes.new.tsx"),
    route(":id", "routes/notes.$id.tsx"),
  ]),
  route("*", "routes/not-found.tsx"),
] satisfies RouteConfig;

The layout: list and guard

TSX
// app/routes/notes.tsx (read-only)
import { NavLink, Outlet } from "react-router";
import type { Route } from "./+types/notes";
import { requireUserId } from "~/auth.server";
import { listNotes } from "~/notes.server";

export async function loader({ request }: Route.LoaderArgs) {
  const userId = await requireUserId(request);
  return { notes: await listNotes(userId) }; // titles and ids only
}

export default function NotesLayout({ loaderData }: Route.ComponentProps) {
  return (
    <div className="split">
      <nav aria-label="Your notes">
        <NavLink to="/notes/new">New note</NavLink>
        <ul>
          {loaderData.notes.map((n) => (
            <li key={n.id}><NavLink to={`/notes/${n.id}`}>{n.title}</NavLink></li>
          ))}
        </ul>
      </nav>
      <main><Outlet /></main>
    </div>
  );
}

Creating, editing and deleting

TSX
// app/routes/notes.$id.tsx (read-only)
export async function loader({ request, params }: Route.LoaderArgs) {
  const userId = await requireUserId(request);
  const note = await getNote(userId, params.id);       // scoped by owner
  if (!note) throw data("Note not found", { status: 404 });
  return { note };
}

export async function action({ request, params }: Route.ActionArgs) {
  const userId = await requireUserId(request);
  const form = await request.formData();

  if (form.get("intent") === "delete") {
    await deleteNote(userId, params.id);               // also scoped by owner
    return redirect("/notes");
  }

  const errors = validateNote(form);
  if (errors) return data({ errors }, { status: 400 });

  await updateNote(userId, params.id, { title: String(form.get("title")), body: String(form.get("body")) });
  return { saved: true };
}

export default function NotePage({ loaderData, actionData }: Route.ComponentProps) {
  return (
    <>
      <Form method="post">
        <input name="title" defaultValue={loaderData.note.title} aria-invalid={Boolean(actionData?.errors?.title)} />
        {actionData?.errors?.title && <p role="alert">{actionData.errors.title}</p>}
        <textarea name="body" defaultValue={loaderData.note.body} />
        <button name="intent" value="save">Save</button>
      </Form>
      <Form method="post">
        <button name="intent" value="delete">Delete</button>
      </Form>
    </>
  );
}

Both forms post to the same action, and the intent field tells them apart. Because actions also run after login checks and ownership scoping, a hand-crafted POST to somebody else's ID simply finds nothing.

Try the model

The stand-in below implements the same behaviors with in-memory data: registration with hashed passwords, sessions, a layout loader, validation, ownership scoping, redirects and a boundary for 404s. Read the output as a log of the HTTP conversation.

Node.js Stand-in: the notes app as functions

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Extend it

  1. Search: add ?q= to the /notes loader and filter by title; read the term with useSearchParams and keep it in the URL.
  2. Pending UI and optimistic delete with a fetcher, as in Prefetching and Pending UI.
  3. Flash messages after save and delete using session.flash.
  4. A database: replace the arrays with SQLite or PostgreSQL behind the same function names.
  5. Tests: write loader and action tests with the approach from Testing Loaders and Actions.
  6. Deploy with the checklist in Deployment Targets.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why does every note loader and action scope its query by the current user's ID?

    Ownership must be enforced on the server for every read and write.
  2. 2. Two forms post to the same action. How does the action tell them apart?

    A named submit button with a value, such as intent, is the standard way to distinguish operations.
  3. 3. What should the action return after successfully creating a note?

    Post/redirect/get keeps the browser history clean and avoids duplicate submissions.
  4. 4. Why is the notes list loaded in the layout route?

    Nested routes reload only what changed, so selecting a note does not refetch the list unless it is revalidated.

Summary

  • Plan URLs first, then map each to a route module with a loader and an action.
  • Guard the layout and every loader and action; scope every query by owner.
  • Validate input, return errors with data(), and redirect after successful mutations.
  • Use boundaries for 404 and unexpected errors; extend with search, flash messages, a database and tests.

Next: Project: A Blog with Admin Login