React: From Components to Production · Sharing State and Structuring Apps
Structuring a React Codebase
Organize by feature, define boundaries and keep components maintainable.
Organize larger apps by feature (features/orders/, features/auth/) with each feature owning its components, hooks, API calls and types, plus a shared/ folder for UI primitives and utilities. Keep components small, separate data-fetching hooks from presentational components, use absolute imports (@/features/...), and enforce boundaries with lint rules. Colocate tests and stories next to components.
src/
app/ # router, providers, layout
features/
orders/
api.ts # fetchers + query hooks (useOrders, useCreateOrder)
OrdersPage.tsx
OrderTable.tsx
OrderTable.test.tsx
types.ts
auth/
shared/
ui/ # Button, Modal, Field
lib/ # formatting, http client
Common pitfalls
- Folders by type only (
components/,hooks/) that become dumping grounds. - Deep relative imports (
../../../). - Features importing each other's internals instead of public entry points.
Key points
- Group by feature
- Shared UI primitives in one place
- Colocate tests
Further reading: react.dev: Thinking in React
Practice questions
Why organize by feature instead of by file type?
Answer
Changes usually touch one feature; keeping its components, hooks and API code together makes them easier to find, change and delete, and clarifies ownership.
What belongs in a shared/ui folder?
Answer
Generic, reusable presentational components (buttons, inputs, modals) with no feature-specific business logic.
Open in the app to mark lessons complete, take notes and track your progress.