Sub topic 1
Why Structure Matters as a Codebase Grows
Prerequisites
Assume you know React components, props, hooks, and JavaScript or TypeScript modules. You should also be familiar with npm and a React bundler such as Vite or webpack.
Why Structure Matters as a Codebase Grows
A small application can survive loose organization because one person remembers where everything is. As the application grows, that memory becomes a dependency. Developers spend more time locating code, guessing ownership, and avoiding changes that might break unrelated features.
Good structure reduces the cost of change. It makes related code easy to find, limits which modules can depend on one another, and gives new team members useful rules to follow. Structure does not remove complexity. It puts complexity in predictable places.
Organizing by File Type Versus by Feature
A type-based structure groups all files of the same kind:
src/
components/
hooks/
pages/
services/
tests/This works for a small project. In a large project, implementing one feature requires navigating many distant folders.
A feature-based structure keeps the code for one business capability together:
src/
features/
cart/
components/
hooks/
api.ts
index.ts
orders/
components/
hooks/
api.ts
index.ts
app/
shared/Use app for application wiring, features for business capabilities, and shared for genuinely reusable code. Do not place code in shared merely because two files currently use it.
Naming and Colocating Related Files
Use names that describe behavior or domain concepts. CartList.tsx, useCart.ts, and cartApi.ts communicate more than generic names such as List.tsx, useData.ts, and service.ts.
Colocate files that change together. A component's test, styles, and local helpers can live beside the component. This keeps maintenance work local and makes ownership visible.
Building Feature Modules
Treat each feature as a module with a small public API. Its index.ts exports supported components, hooks, and types. Other features import from that entry point rather than reaching into internal folders.
// src/features/cart/index.ts
export { CartList } from "./components/CartList";
export { useCart } from "./hooks/useCart";
export type { CartItem } from "./types";This boundary lets you reorganize internals without changing every consumer.
Exercise
Where should a reusable button used by several unrelated features live?
Show answer
In shared UI, provided that it contains no feature-specific behavior or state.