Sub topic 2
Organizing by file type versus by feature
This chapter assumes you can build React components with props and hooks, you know import and export in TypeScript or modern JavaScript, and you have worked in a small or medium React app. The chapter compares the two most common ways to lay out folders. It then gives criteria for choosing between them.
The two layouts
Most React projects start with a layout grouped by file type. Each folder holds one kind of file, whatever feature the file belongs to.
src/
components/
CartList.tsx
OrderTable.tsx
Button.tsx
hooks/
useCart.ts
useOrders.ts
api/
cart.ts
orders.ts
utils/
formatDate.ts
types/
cart.ts
orders.tsA layout grouped by feature puts everything for one feature in one folder. App wiring and code used everywhere sit in 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.tsBoth layouts contain the same code. Only the grouping differs. The grouping decides which files you see next to each other, and that shapes how you work in the codebase.
Why the type-based layout stops scaling
A type-based layout works well while the app has a handful of screens. Three problems appear as it grows.
- One change touches many folders. Adding a coupon field to the cart means editing a component, a hook, an API function, and a type. Each lives in a different top-level folder, so you open files in four places.
- Folders become long lists. After a year,
components/can hold 200 files. Names such asCartListandOrderTablesit beside unrelated files, and you can't tell which components belong together. - Dependencies are invisible. Any file can import any other file. Nothing tells you that
useOrdersis used only by the orders screens, so you can't safely delete or change it without searching the whole project.
The third problem is the most costly. Ownership is unclear, so code is rarely removed, and code that can't be removed accumulates.
What the feature-based layout changes
A feature is a part of the product a user can name, such as the cart, orders, or settings. Putting its files together has three effects.
- Related files change together, so they sit together. Most edits stay inside one folder, and a pull request reads as one unit.
- The folder has an edge. You can ask what the cart feature exposes and what it imports. Later chapters turn this edge into a public API and enforce it with lint rules.
- Deleting a feature is a folder delete. If nothing outside imports from
features/cart, removing it is safe and quick.
The app/ folder holds code that wires features into a running application, such as the root component and the route table. The shared/ folder holds code that has no feature in it, such as a Button or a date formatter. Keeping those two separate stops feature folders from depending on each other through the back door.
Seeing the difference in imports
Imports show the layout most clearly. In the type-based layout, a cart screen pulls from four unrelated folders:
// src/components/CartList.tsx (type-based)
import { useCart } from "../hooks/useCart";
import { formatDate } from "../utils/formatDate";
import type { CartItem } from "../types/cart";
import { Button } from "./Button";In the feature-based layout, the same component imports its own feature's files by short relative paths and shared code from shared/:
// src/features/cart/components/CartList.tsx (feature-based)
import { useCart } from "../hooks/useCart";
import type { CartItem } from "../types";
import { formatDate } from "../../../shared/lib/formatDate";
import { Button } from "../../../shared/ui/Button";The first two imports now stay inside one feature. The last two cross a boundary, and they are the imports worth noticing in review. The long ../../../ paths are awkward. The path alias chapter fixes that, so ignore the awkwardness for now.
Where the feature-based layout goes wrong
Feature folders have their own failure modes. Knowing them early saves a migration later.
Features that import each other
If orders imports from deep inside cart, and cart imports from orders, you have a cycle. The folders look separate, but the code is still one tangled unit. A feature should depend on shared/ and, sparingly, on another feature's public exports. If two features need each other constantly, they are probably one feature.
A `shared/` folder that becomes a dumping ground
Developers move code to shared/ as soon as a second feature wants it. This fills shared/ with code that really belongs to one feature but is tangled with another. A safer rule is to start code inside the feature that uses it. Move it to shared/ only when two features need it and it has no knowledge of either feature's domain.
Features that are too small or too large
A feature per component creates hundreds of tiny folders. A single dashboard feature that holds the whole product defeats the purpose. Aim for a feature that one small group of developers could own and that matches a noun in your product's vocabulary.
Type folders inside features
The components/ and hooks/ folders inside features/cart are fine. Inside one feature the file count is small, so sub-grouping by type costs little. For a small feature, you can keep all files flat in the feature folder.
Choosing between them
Neither layout is correct in every case. These criteria help you decide.
| Situation | Better fit |
|---|---|
| Under about 20 components, one or two developers | By type |
| Several distinct product areas | By feature |
| Multiple teams working in one repository | By feature |
| Prototype or short-lived tool | By type |
| Code you expect to delete or extract later | By feature |
A useful test is to pick the last three pull requests and count how many top-level folders each one touched. If most touch three or more, the type-based layout is costing you time.
Common mistakes when switching
- Moving files without changing imports to match. A folder move that leaves cross-feature deep imports in place gives you the new names with the old coupling.
- Creating
features/commonorfeatures/misc. These folders hold unrelated code and recreate the type-based problem in a new location. Shared code belongs inshared/. - Putting routes inside each feature and also in
app/. Pick one owner. The usual choice is a route table inapp/that imports page components from each feature. - Treating the layout as a rule instead of a tool. If a rule makes a change harder, check whether the rule's purpose still applies.
Exercises
Exercise
A developer adds a "gift message" field to checkout. In a type-based layout, they edit components/CheckoutForm.tsx, hooks/useCheckout.ts, api/checkout.ts, and types/checkout.ts. Describe where these four files live in a feature-based layout and name one benefit.
Show answer
All four live under src/features/checkout/, for example components/CheckoutForm.tsx, hooks/useCheckout.ts, api.ts, and types.ts. A benefit is that the change stays in one folder, so the pull request is easier to review and does not scatter edits across the project. Another benefit is that other developers can see which files belong to checkout.
Exercise
Your team has a shared/ folder. A new formatCartTotal function is used only by the cart feature today, and a second feature may need it next quarter. Where should it go now, and why?
Show answer
Keep it inside features/cart. Code should start in the feature that uses it. Moving it to shared/ before a second user exists adds domain-specific code to a folder meant for code with no feature knowledge. If the second feature needs it later, move it then, and check whether it is generic enough for shared/. If it still depends on cart concepts, expose it through the cart feature's public exports instead.