Sub topic 2
Organizing by file type versus by feature
This chapter assumes you can build a React app with components and hooks, and that you know import and export in TypeScript. You should also know how to run a Vite or webpack project.
Two ways to group files
Every project has to decide where a new file goes. There are two common answers.
- By file type. Files are grouped by what they are:
components/,hooks/,api/,utils/. - By feature. Files are grouped by what they are for:
cart/,orders/,settings/. Each feature folder holds its own components, hooks, and API code.
Grouping by type works well in a small app. It breaks down as the app grows.
What goes wrong with type folders
Suppose you change how the cart works. In a type-based layout, the files you need are spread out:
components/CartList.tsxhooks/useCart.tsapi/cart.tstypes/cart.ts
You open four folders to make one change. Each folder also holds files from every other feature, so components/ may grow to hundreds of files. Nothing tells you which files belong together or which are safe to delete. Any file can import any other, so dependencies spread without anyone deciding on them.
The feature-based layout
A feature-based layout keeps everything for one feature together. App wiring and code shared by all features get their own top-level folders.
src/
app/
App.tsx
routes.tsx
features/
cart/
components/
CartList.tsx
CartList.test.tsx
hooks/
useCart.ts
api.ts
index.ts
orders/
components/
hooks/
api.ts
index.ts
shared/
ui/
Button.tsx
lib/
formatDate.tsThe three top-level folders have different jobs:
| Folder | Holds | Depends on |
|---|---|---|
| `app/` | Root component, routes, providers | Features and shared code |
| `features/` | One folder per product feature | Shared code |
| `shared/` | Code with no knowledge of any feature | Nothing in this project |
The dependency direction matters. shared/ must not import from features/, or it stops being shared.
Notice what stays inside the feature
Each feature still has components/ and hooks/ folders inside it. Type folders are fine at this smaller scale, where each holds a handful of files. The change is the first level of grouping, not a ban on type folders.
The test file CartList.test.tsx sits next to CartList.tsx. When you delete the component, you see the test and delete it too.
Deciding where code goes
Use these questions when you add a file:
- Does it serve one feature? Put it in that feature's folder.
- Do two features use it? Wait before moving it. Two uses can be a coincidence.
- Do many features use it, and does it know nothing about any of them? Move it to
shared/.
Moving code to shared/ too early is a common mistake. A "shared" component that needs a cart-specific prop for one caller is really a cart component.
Common errors
- Features importing each other's internals. If
ordersimportscart/hooks/useCart, the two features are now tied together. A later chapter covers theindex.tsfile that limits this. - A
shared/folder that becomes a dumping ground. Keep the rule above: nothing in it may know about a feature. - Reorganizing everything at once. You can move one feature at a time. The migration chapter covers this.
Exercise
A project has components/CartList.tsx, components/OrderTable.tsx, hooks/useCart.ts, and hooks/useOrders.ts. Name the folder layout you would move to, and say where formatDate.ts, used by both cart and orders, should go.
Show answer
Create features/cart/ and features/orders/, each with its own components/ and hooks/ folders, and move each file to its feature. formatDate.ts goes in shared/lib/, because two features use it and it knows nothing about either one.