Skip to content
ClapelloNotes by Barath
ReactCh 17 · 6/6
Core4 min readUpdated

React / 99

Project structure that scales

TL;DR

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/
Highlighted: every file you touch to change the cart. Grouped by type, they're spread over four folders. Grouped by feature, they sit together.

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, a Modal, formatPrice) live in shared folders such as components/ui/ and lib/.
  • 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 menu needs something from cart, 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.tsx beside CartPage.tsx, not in a far-off tests/ folder.
  • CartPage.module.css beside the component it styles.
  • A hook only CartPage uses 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:

src/features/cart/index.ts
export { CartPage } from "./CartPage";
export { useCart } from "./useCart";
export { TAX_RATE } from "./tax";
src/App.tsx
import { CartPage } from "./features/cart";

Neat, but it has costs:

  1. 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.)
  2. 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

src/features/cart/CartPage.tsx
import { useCart, TAX_RATE } from "."; // loops back to index.ts

Do

Inside the feature: import siblings directly

src/features/cart/CartPage.tsx
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.