Sub topic 3
Naming conventions and colocating related files
This chapter assumes you can write React components and hooks, use TypeScript or modern JavaScript modules, and have built a small or medium React app. It covers how to name files and where to put files that change together.
Why naming and colocation matter
A file name is the first thing a teammate sees in a search box, a stack trace, or a pull request. Consistent names make files easy to find. Colocation means keeping files that change together in the same folder. When a component, its test, and its hook sit side by side, you edit them in one place. You also notice when you delete a component and leave dead files behind.
Naming conventions
Pick one rule for each kind of file and apply it everywhere. The table shows a common set.
| Kind of file | Convention | Example |
|---|---|---|
| Component | PascalCase, matches the exported component | `CartList.tsx` |
| Hook | camelCase, starts with `use` | `useCart.ts` |
| Test | Same name as the file under test, plus `.test` | `CartList.test.tsx` |
| Plain module | camelCase | `formatDate.ts` |
| Types | camelCase, or `types.ts` inside a feature | `types.ts` |
| Folders | lowercase | `features/cart` |
Three habits prevent common errors:
- Name a file after the one thing it exports.
CartList.tsxexportsCartList. - Use
.tsxonly when the file contains JSX. Hooks and utilities that return no JSX should use.ts. - Avoid vague names such as
utils.tsorhelpers.ts. They grow into dumping grounds. PreferformatDate.tsorparseQuery.ts.
Case-sensitivity is a frequent source of bugs. macOS and Windows file systems usually ignore case, while Linux does not. An import of ./cartList works on a laptop and fails in a Linux CI job. Enable forceConsistentCasingInFileNames in tsconfig.json to catch this early.
Colocating related files
Place a file next to the code that uses it. Move it up to a shared folder only when a second feature needs it.
features/
cart/
components/
CartList.tsx
CartList.test.tsx
CartList.module.css
hooks/
useCart.ts
useCart.test.ts
api.ts
types.ts
index.tsThe test and the styles for CartList live beside it. Rename or delete CartList.tsx and the related files are right there in the same folder.
Some teams go one step further and give each larger component its own folder:
CartList/
CartList.tsx
CartList.test.tsx
CartList.module.css
index.tsThis helps when a component has several private files. It adds a folder level and many files called index.ts, which makes editor tabs ambiguous. Start with flat files and create a component folder only when a component collects three or more private files.
When not to colocate
Colocation has limits. Code used by many features belongs in shared/, not in the feature that wrote it first. If feature B imports a file from inside feature A, move that file to shared/ or reconsider the boundary. Later chapters cover public APIs and lint rules that enforce this.
Exercise
A teammate adds src/utils/helpers.ts containing a date formatter and a currency formatter, and only the cart feature uses them. Name two changes that follow this chapter's guidance.
Show answer
Split the file into specific names such as formatDate.ts and formatCurrency.ts, and move them into the cart feature (for example features/cart/) because only that feature uses them. Move them to shared/lib/ only when a second feature needs them.
Exercise
Your app builds on macOS but CI on Linux fails with "Cannot find module './cartList'", although the file is CartList.tsx. What is the cause, and which setting helps catch it locally?
Show answer
macOS file systems are usually case-insensitive and Linux is case-sensitive, so the wrong-case import only fails on Linux. Setting forceConsistentCasingInFileNames to true in tsconfig.json reports the mismatch during local type checking.