You built a complete To-Do app: components, state, lists, effects, and a real backend with login. This lesson collects the habits we used into a checklist, plus the mistakes almost every beginner makes, and a cheat sheet to keep.
The folder structure we ended with
src/
βββ main.tsx β starts React with the router
βββ router.tsx β every URL β page, in one place
βββ auth.ts β save / read / clear the login token
βββ types.ts β shared types (Task, Filter)
βββ index.css β styles
βββ api/client.ts β every request to the backend
βββ hooks/useTasks.ts β data logic, as a custom hook
βββ pages/ β one file per URL
β βββ LoginPage.tsx TasksPage.tsx TaskPage.tsx NotFoundPage.tsx
βββ components/ β reusable pieces
βββ Layout.tsx RequireAuth.tsx
βββ TaskForm.tsx TaskList.tsx TaskItem.tsx
βββ FilterBar.tsx LoginForm.tsx| Folder | Put here | Rule of thumb |
|---|---|---|
| components/ | Reusable UI pieces | Mostly props in, JSX out |
| pages/ | Whole screens, one per route | Combine components and hooks |
| hooks/ | useSomething functions | State + effects you want to reuse or keep out of components |
| api/ | fetch calls | The only place that knows the backend URL |
10 best practices
- Keep components small. If a component does many jobs or passes 200 lines, split it.
- One component per file, named like the component (
TaskItem.tsx). - Type your props with
type Props. TypeScript then checks every use. - Put state where it's needed: in the closest common parent, no higher.
- Never mutate state: new arrays and objects with
...,mapandfilter. - Don't store what you can calculate: filtered lists and counts come from state.
- Stable keys in lists: ids, not indexes.
- useEffect only for the outside world, with every used value in the dependency array.
- Handle loading and errors for every request.
- API calls in one place (
api/), data logic in custom hooks.
The rules of hooks
function TaskPage({ open }: { open: boolean }) {
// β
hooks at the top level of the component
const [filter, setFilter] = useState("all");
// β never inside conditions, loops or nested functions
// if (open) { const [x, setX] = useState(0); }
// β
custom hooks follow the same rule
const { tasks } = useTasks();
// ...
}Mistakes almost every beginner makes
| Mistake | Symptom | Fix |
|---|---|---|
| tasks.push(task) | Screen doesn't update | setTasks([...tasks, task]) |
| onClick={save()} | Runs on every render, maybe an endless loop | onClick={save} or onClick={() => save(id)} |
| key={index} | Wrong row changes after a delete | key={task.id} |
| Missing e.preventDefault() | Page reloads on submit, state is lost | Call it first in onSubmit |
| Effect without dependencies | Runs after every render, fetches forever | Add [] or the values it uses |
| Reading state right after setState | Shows the old value | Use the value you computed, or setX((cur) => β¦) |
| class instead of className | Warning in the console, no styles | className |
Build and deploy
# check types and build the app for the web$bun run build# try the built app locally$bun run preview
bun run build creates a dist folder of plain HTML, CSS and JavaScript. Upload it to any static host (Cloudflare Pages, Netlify, Vercel). Set VITE_API_URL in the host's settings to your live API's address, and allow your site's address in the API's CORS settings.
The cheat sheet
// Component with typed props
type Props = { title: string; onDelete: (id: number) => void };
export function Row({ title, onDelete }: Props) { return <li>{title}</li>; }
// State
const [items, setItems] = useState<Item[]>([]);
setItems([...items, item]); // add
setItems(items.map((i) => (i.id === id ? { ...i, done: true } : i))); // change
setItems(items.filter((i) => i.id !== id)); // delete
// Lists and conditions
{items.map((i) => <Row key={i.id} title={i.title} onDelete={remove} />)}
{error && <p>{error}</p>}
{loading ? <p>Loadingβ¦</p> : <List />}
// Effects
useEffect(() => { /* outside world */ return () => { /* cleanup */ }; }, [deps]);
// Forms
<form onSubmit={(e) => { e.preventDefault(); /* β¦ */ }}>
<input value={text} onChange={(e) => setText(e.target.value)} />
</form>Where to go next
- Practise: add an βedit titleβ button to each task (hint: PATCH with a new title).
- Learn next: Tailwind CSS, testing and deployment. These are coming to this course.
- The backend side: if you skipped it, build the API yourself in the Node.js Backend course.
Key takeaways
- βYou built a full To-Do app in React, connected to a real backend.
- βA simple folder structure (components, pages, hooks, api) keeps it organised.
- βMost bugs come from a few mistakes: mutating state, bad keys, wrong effect dependencies.
- βThe rules of hooks: only at the top level of components and custom hooks.
- βbun run build creates static files you can host anywhere.
Frequently asked questions
What is the best folder structure for a React app?
For small and medium apps: components/ for reusable pieces, pages/ for whole screens, hooks/ for custom hooks, api/ for talking to the backend, and types.ts for shared types. Start simple and split folders only when they get crowded.
What are the rules of hooks?
Only call hooks (useState, useEffect, your own useSomething) at the top level of a component or custom hook: never inside if statements, loops or nested functions, and never in normal functions. React relies on hooks being called in the same order every render.
How do I deploy a React app?
Run bun run build. Vite creates a dist folder of plain HTML, CSS and JavaScript. Upload it to any static host, such as Cloudflare Pages, Netlify or Vercel, and set VITE_API_URL to your real API's address there.
What should I learn after this course?
A styling tool like Tailwind CSS, testing with Vitest and Testing Library, and then a framework like Next.js. Most of all: build your own small projects.
Do I need Redux or another state library?
Not to start. useState, lifting state up and custom hooks cover most apps. Reach for a library only when passing state through many layers becomes painful.
π§ Quick check: React best practices
1. Which is a rules-of-hooks mistake?
2. What does bun run build produce?
3. A list shows the wrong item after deleting. What's the likely cause?
4. Where should code that calls the backend live?
Checking your accessβ¦