A minimal demo app: useActionState + useOptimistic (React 19) for list management and form submissions. Posts list, add-post form with optimistic UI, loading and error state, and an in-memory API for the demo.
- React 19
- Vite 7
- TypeScript
- Tailwind CSS 4
- ESLint, Prettier, Husky
- Node.js 18+
-
Clone and install
git clone <repo-url> cd optimistic-list-update npm install
-
Run
npm run dev
App: http://localhost:5173. Use “Add Post”; optionally enable “Simulate Fail” to see error handling.
- App -
src/App.tsx: list state viauseState, initial load viauseTransition+useEffect+postsApi.getAll();useOptimisticfor optimistic posts;useActionStatefor the add-post form (action runsaddOptimisticPostthenpostsApi.create()). Passes form action, pending state and error to the form. - Form -
src/components/AddPostForm/: presentational form; receivesonSubmit(form action),isPending,errorfrom App; no form logic inside. - List -
src/components/PostList/,PostItem/: render the (optimistic) posts list. - API -
src/lib/posts.ts: in-memorygetAllandcreate(with optionalsimulateFail). - Types -
src/types/posts.ts:Post,CreatePostInput,AddPostFormState.
| Command | Description |
|---|---|
npm run dev |
Development server |
npm run build |
Type-check + build |
npm run preview |
Preview production |
npm run lint |
ESLint |
npm run format |
Prettier (write) |
npm run format:check |
Prettier (check only) |
Husky is installed and configured via the prepare script. Add a pre-commit hook (e.g. with lint-staged) to run ESLint and Prettier on staged files before each commit if you want the same workflow as in the template.
# Hooks are set up on npm install (via "prepare")MIT (or as needed for your project).