React / 99
Project structure that scales
Group files by feature instead of by type, keep things next to where they're used, and know the traps of index.ts barrel files.
Group by feature
by type · fine when small
- src/
- components/
- CartPage.tsx
- MenuList.tsx
- LoginForm.tsx
- hooks/
- useCart.ts
- useMenu.ts
- utils/
- cartTotal.ts
- tests/
- CartPage.test.tsx
by feature · scales
- src/
- features/
- cart/
- CartPage.tsx
- CartPage.test.tsx
- useCart.ts
- cartTotal.ts
- menu/
- auth/
- components/ui/
- Button.tsx
- lib/
A feature is a slice of the product: cart, menu, auth, checkout. Each gets a folder holding everything it needs:
features/cart/holds the cart's components, hooks, helpers and tests.- Generic pieces used across features (a
Button, aModal,formatPrice) live in shared folders such ascomponents/ui/andlib/. - Promote late. Code moves to a shared folder when a second feature needs it, not before. Guessing what will be reused early fills
shared/with one-off code. - Features stay independent. If
menuneeds something fromcart, import what cart chooses to share, not a file deep inside it.
The payoff: to change a feature you open one folder, and to delete a feature you delete one folder.
Colocate
Colocation means keeping a file next to the code that uses it. Things that change together live together:
CartPage.test.tsxbesideCartPage.tsx, not in a far-offtests/folder.CartPage.module.cssbeside the component it styles.- A hook only
CartPageuses can live in the same file, or next to it. It moves out when something else needs it.
When you rename or delete CartPage, its test and styles are right there to update.
Barrel files: handy, with traps
A barrel is an index.ts that re-exports the files in its folder, so others can import from the folder:
export { CartPage } from "./CartPage";
export { useCart } from "./useCart";
export { TAX_RATE } from "./tax";import { CartPage } from "./features/cart";Neat, but it has costs:
- It loads everything. Importing one name from a barrel makes the dev server and test runner load every file it re-exports, and everything those import. In a big app that slows page loads in development and every test file. The Vite docs recommend importing files directly instead. (Production bundlers usually drop unused exports, so the cost is mostly in development and tests.)
- Circular imports. A file inside the feature that imports from its own barrel creates a loop: barrel →
CartPage→ barrel. Depending on load order, a value can be missing when it's needed:ReferenceError: Cannot access 'TAX_RATE' before initialization.
Don't
Inside the feature: import from your own barrel
import { useCart, TAX_RATE } from "."; // loops back to index.tsDo
Inside the feature: import siblings directly
import { useCart } from "./useCart";
import { TAX_RATE } from "./tax";A sensible rule: either skip barrels and import files directly, or keep one small barrel per feature as its public face, used only from outside the feature. Never export * from deep trees: it hides what's actually public and lets names clash silently.
When to bother
A small app with a dozen components doesn't need any of this. Flat components/ and hooks/ folders are fine. Restructure when you feel the pain: when one change touches many folders, or you can't find things. Frameworks fix part of the layout for you (Next.js routes live in app/, for example). Keep your feature code beside that, not mixed into it.
Gotchas
Recall
Can you answer these?
0/5
Answer out loud (or in your head) before you tap. That's what makes it stick.
Saved on this device only. No login.
