React Architecture
Feature module structure
Organizing files by technical type (components/, hooks/, services/ at the root) forces developers to jump across unrelated folders when changing a single capability. Instead, group code by domain feature so everything related to billing, authentication, or reporting lives together and exposes a narrow public entry point (index.ts):
src/
app/ # Routing, providers, global shell
shared/ # Truly generic UI primitives and utilities
features/
billing/
components/ # Feature-specific UI
hooks/ # Stateful orchestration for billing
services/ # API transport and mappers
model/ # Pure domain calculations and types
index.ts # Public surface imported by other features
State architecture
Classify state by ownership and lifetime rather than putting everything into a single global store:
- Local UI state: Modals, tabs, and form drafts stay inside the component or feature that renders them.
- Server cache state: Remote API data has caching, staleness, and retry semantics—manage it with dedicated server-state hooks or a query cache rather than duplicating it into client stores.
- Shared client state: Session identity, theme, or cross-route workspace filters belong in focused Context providers or lightweight stores.
Dependency boundaries
Enforce a one-way dependency graph: app may import from features and shared; features may import from shared; and shared must never import from features. Keep pure domain rules (such as tax or discount calculations) in plain JavaScript/TypeScript functions under model/ so they can be unit-tested without mounting React components.
Scaling strategy
Evolve structure pragmatically as the codebase matures:
- Start with feature folders and lint rules (such as ESLint import boundaries) that block cross-feature internal imports.
- Promote code to
shared/only after two or more features genuinely share the same abstraction, avoiding premature generalization. - Document architectural decisions (ADRs) near the code when introducing new state libraries or routing conventions.
Practice
Restructure a multi-page sample React app into app/, shared/, and two features/ modules with barrel index.ts files, ensuring domain logic is isolated in pure functions.