Sub topic 4
Building feature modules
This chapter assumes you can write React components and hooks, use TypeScript import and export, and have built a small or medium React app. It shows how to turn a feature, such as a shopping cart, into a self-contained module.
What a feature module is
A feature module is a folder that holds everything one product feature needs: its components, hooks, data access, types, and tests. Code that is only used by that feature lives inside the folder. Code that many features use goes in shared/.
The folder has one entry point, index.ts. Other parts of the app import from that file and nothing else.
A typical layout
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.tsEach top-level folder has one job:
| Folder | Contents | May import from |
|---|---|---|
| `app/` | Root component, routes, providers | `features/`, `shared/` |
| `features/` | One folder per feature | `shared/`, and other features only through their `index.ts` |
| `shared/` | Generic UI and utilities | Only `shared/` itself |
Dependencies point in one direction: app uses features, and features use shared. shared never imports from a feature.
Writing the public entry point
The index.ts file lists what the feature offers to the rest of the app.
// src/features/cart/index.ts
export { CartList } from "./components/CartList";
export { useCart } from "./hooks/useCart";
export type { CartItem } from "./types";Anything not exported here is private. You can rename api.ts or split a component without touching code outside the folder.
Other code then imports like this:
import { CartList, useCart } from "@/features/cart";The @/ prefix is a path alias. Setting it up is covered in a later chapter.
Deciding what goes in a feature
Use these checks when you place a file:
- If only one feature uses it, keep it in that feature.
- If two features use it and it is generic, such as a date formatter, move it to
shared/. - If two features use it and it contains business rules, consider whether one feature should export it, or whether the two features are really one.
Do not move code to shared/ just because it might be reused. Wait until a second real use appears.
Common errors
- Deep imports. Writing
import { CartList } from "@/features/cart/components/CartList"skips the public entry point. It works, but it ties the caller to the cart's internal layout. Lint rules can catch this, as shown in a later chapter. - Exporting everything. If
index.tsre-exports every file, the boundary is meaningless. Export only what other code needs. - Circular dependencies. If
cartimportsordersandordersimportscart, the two are tightly coupled. Move the shared part intoshared/, or merge the features. - Feature code in
shared/. A component that knows about carts belongs infeatures/cart, even if the page uses it in several places.
Exercise
The orders feature needs a formatCurrency function that cart also uses. Where should it live, and how should orders import it?
Show answer
Put it in shared/lib/formatCurrency.ts, because it is generic and two features use it. Both features import it from shared, for example import { formatCurrency } from "@/shared/lib/formatCurrency". Neither feature imports it from the other.
Exercise
A teammate writes import { useCart } from "@/features/cart/hooks/useCart" in the checkout page. What is wrong, and what is the fix?
Show answer
It reaches past the cart feature's public entry point into its internals. Change it to import { useCart } from "@/features/cart". If useCart is not exported from index.ts, add it there only if other code should really use it.