Sub topic 1
Why Structure Matters as a Codebase Grows
Prerequisites
This chapter assumes you can write React function components with props and hooks, and that you know basic import and export syntax in TypeScript or modern JavaScript. You should also have built a small or medium React app with npm and a bundler such as Vite or webpack.
What goes wrong as code grows
A new React project usually starts with a few folders and a handful of files. Nobody has to think about where code goes. Growth changes that. When the app has dozens of screens and several developers, three problems appear.
- Slow navigation. One change, such as adding a field to a cart item, touches files in
components/,hooks/,api/, andtypes/. You spend time finding the files before you can change them. - Hidden coupling. Any file can import any other file. A component deep in the order screen ends up importing a helper from the cart screen. Later, someone edits that helper and the order screen breaks.
- Fear of deletion. Nobody knows what still uses a file, so old code stays. The codebase only gets larger.
Structure does not make the app run faster. It makes the code cheaper to read, change, and remove.
The goals of a good structure
Good structure aims at four goals.
- Findability. A developer can guess where code lives from what it does.
- Locality of change. A typical change touches one area of the code, not many.
- Explicit boundaries. Each area exposes a small, deliberate set of names. Other areas use only those names.
- Safe removal. You can delete or rewrite an area and know what it affects.
The rest of this tutorial builds these goals into concrete rules. Keep them in mind, because they are the test for every later decision.
Coupling shown in code
This example shows hidden coupling. The orders screen reaches into the internals of the cart feature.
// src/features/orders/components/OrderSummary.tsx
import { calcCartTotal } from "../../cart/utils/calcCartTotal";
export function OrderSummary({ items }: { items: { price: number; qty: number }[] }) {
return <p>Total: {calcCartTotal(items)}</p>;
}The relative path ../../cart/utils/calcCartTotal points at a private file. If the cart team renames or moves that file, OrderSummary breaks, even though nobody touched the orders feature. A later chapter shows how a public API and a lint rule prevent this.
How dependencies should flow
The diagram below shows the direction you want. Pages use features, features use shared code, and shared code depends on nothing in the app.
Notice that there is no arrow between the cart and orders features. When two features need to cooperate, they do it through a public API or through the page that uses both.
Common mistakes
- Restructuring too early. A five-file app does not need layers. Add structure when the pain appears.
- Copying a template without the goals. A folder layout is a means. If it does not improve findability or locality of change for your team, change it.
- Treating shared code as a dumping ground. Moving a file to
shared/to avoid a circular import hides the coupling and does not remove it.
What comes next
The next chapters compare organizing by file type with organizing by feature, then add feature modules, public APIs, layers, and lint rules.
Exercise
A developer renames cart/utils/calcCartTotal.ts, and the orders screen stops compiling. Which structural goal was violated, and what is the underlying cause?
Show answer
Explicit boundaries were violated. The orders feature imported a private file from the cart feature, so it was coupled to the cart's internals. A public API for the cart feature would have let the cart team change internal files without breaking other features.
Exercise
Why is moving a helper to shared/ to break a circular import a weak fix?
Show answer
It removes the visible import cycle but keeps the real dependency between the two features. The helper now sits in a place that every feature can use, and shared code becomes a catch-all with unclear ownership.