Skip to main content

TodoMVC

TodoMVC as a full-stack app: a Hono API and the React client run in one Vite dev server. The server keeps the todos in memory and answers after 400 ms, so you can see every change show up before it is saved.

intermediateoptimisticuseSubmithonoserver
import { useSyncExternalStore } from "react";

import type { Todo } from "../../api/todos";

export type Filter = "all" | "active" | "completed";

/** TodoMVC keeps the filter in the hash: `#/`, `#/active` and `#/completed`. Back, reload and links keep it. */
const readFilter = (): Filter => {
const hash = window.location.hash.replace("#/", "");
return hash === "active" || hash === "completed" ? hash : "all";
};

const subscribe = (listener: () => void) => {
window.addEventListener("hashchange", listener);
return () => window.removeEventListener("hashchange", listener);
};

export const useFilter = () => useSyncExternalStore(subscribe, readFilter);

export const filterTodos = (todos: Todo[], filter: Filter) => {
if (filter === "active") return todos.filter((todo) => !todo.completed);
if (filter === "completed") return todos.filter((todo) => todo.completed);
return todos;
};
Run locallynpx tiged BetterTyped/hyper-fetch/examples/apps/todomvc hyper-fetch-apps-todomvc && cd hyper-fetch-apps-todomvc && npm install && npm run dev
What it shows
  1. One project, two sides: Hono serves /api inside the Vite dev server, the client imports only the server's types (Hono fullstack).
  2. Optimistic writes: add, toggle, rename, delete, toggle all and clear completed change the list at once (Optimistic updates).
  3. Rollback on failure: a rejected write puts the todo back, then core refetches the list from the server (Undo delete).
  4. Filters in the URL: #/active and #/completed filter the cached list, so switching costs no request (Filters in the URL).
  5. Save status: useIsSubmitting and request events report every write in one place (CRUD).

How it works​

  1. server/app.ts is a Hono app with the TodoMVC routes under /api. vite.config.ts adds @hono/vite-dev-server, which sends every /api request to that app while vite runs. Errors go out as { message }.

  2. src/api/todos.ts imports the Todo type from the server with import type, so both sides share it and no server code reaches the browser. The requests are typed by hand. HonoSchema from @hyper-fetch/hono can derive them from the routes instead, see Hono fullstack.

  3. Every write owns its optimistic change. updateTodo covers both the checkbox and the rename, and its rollback puts the old todo back where it was:

    optimistic(getTodos)
    .patch(params.todoId, payload)
    .rollback(({ data, previous }) => restore(data, previous, Number(params.todoId)));
  4. createTodo inserts a todo with a temporary negative id and swaps it for the saved one in .commit(). While the id is negative, TodoItem disables the todo.

  5. toggleAllTodos and clearCompletedTodos change the whole list with .set() and have no rollback. If they fail, core refetches the list and the server's state replaces the change.

  6. The server stores every write, so core refetches getTodos after the last write settles. Delete a todo with curl -X DELETE localhost:5173/api/todos/2, then toggle it in the app: the toggle fails with 404, is undone, and the refetch removes the todo.

  7. useFilter() reads the filter from the URL hash with useSyncExternalStore. TodoApp filters the one cached list, so the counter and Clear completed always see every todo.

  8. SaveStatus shows useIsSubmitting(client) while writes are running, and the last error from client.requestManager.events.onResponse.

When to use it
  • Small full-stack apps and prototypes where the API lives next to the client.
  • Lists of small changes that almost always succeed, such as todos, checklists and settings.
  • As a starting point for a TypeScript monorepo that shares types between server and client.

Summary

Hono runs inside Vite, and the client shares its Todo type through a type-only import. Every write carries its optimistic change, core rolls back failures and refetches the list from the server, and the hash-based filters only change what is rendered.