React
Chapter 13: Architecture & Best Practices — Enterprise folder structure, Smart vs Dumb components, state colocation, compound components, and clean code principles.
1. Feature-Based Enterprise Folder Architecture
As applications scale beyond 50 components, grouping files by technical layer (all components in one folder, all hooks in another) creates massive cognitive overhead. Modern enterprise applications organize by Features (Domain-Driven Design):
src/
├── assets/ # Shared global branding icons, images, fonts
├── components/ # Shared atomic UI primitives (Button, Modal, Input)
├── context/ # Global application contexts (AuthContext, ThemeContext)
├── hooks/ # Shared global custom hooks (useLocalStorage, useDebounce)
├── services/ # API client configuration & Axios/fetch instances
├── features/ # Domain-specific feature modules
│ ├── auth/ # Authentication domain
│ │ ├── components/ # LoginForm.jsx, RegisterCard.jsx
│ │ ├── hooks/ # useAuthMutation.js
│ │ ├── services/ # authApi.js
│ │ └── types/ # auth.types.ts
│ ├── checkout/ # Checkout & Billing domain
│ └── dashboard/ # Analytics & Reporting domain
├── layouts/ # Root wrappers (AppLayout, DashboardLayout)
├── routes/ # AppRoutes.jsx router definition
├── App.jsx # Root composition root
└── main.jsx # Entry point
2. Container vs Presentational (Smart vs Dumb) Pattern
Presentational ("Dumb") Components
Pure visual styling
- Focuses exclusively on how things look.
- Receives data strictly via
propsand triggers callbacks. - Contains zero side-effect network calls; completely reusable.
Container ("Smart") Components
Business logic orchestrator
- Focuses on how things work.
- Fetches data, manages state, coordinates custom hooks.
- Renders Presentational components and supplies them with data.
3. State Categorization & State Colocation
🎯 The Golden Rule of State Colocation: "Keep State as Close to Where It's Used as Possible"
Do NOT dump all state into a global Redux/Context store! Categorize your state into 4 layers:
1. Local UI State (useState): Dropdown open/close, modal visibility, hover states.
2. Lifted State (Props): State shared by two sibling components placed in their common parent.
3. Global App State (Context): Authenticated user, Theme, Localization language.
4. Server Cache State (React Query / TanStack): Fetched API data, caching, pagination, optimistic updates.
4. Hands-On Practice Challenges
Master enterprise React architecture, container/presentational separation, and compound component systems. Click "Solve Challenge" to open the workspace!
Challenge 1: Container vs Presentational Data Separator
Write a container processing function separateSmartDumb(rawUserList, filterQuery) that isolates business logic (filtering, sorting) and outputs clean, presentational props ready for rendering.
- Filter
rawUserListby case-insensitivefilterQuery. - Map matched items into presentational props:
{ avatarUrl, title, subtitle }. - Ensure the function does not produce UI markup directly.
[ { avatarUrl: "https://avatars.example.com/1", title: "Anshuman", subtitle: "Staff Eng • Frontend" } ]
separateSmartDumb to isolate business transformations from presentation.
Challenge 2: State Categorization Advisor
Implement an architectural decision helper categorizeState(stateKeyName) that categorizes state keys into their optimal React storage location (Local UI, Lifted, Global Context, or Server Cache).
- Map UI state (
isModalOpen) toLOCAL_UI_STATE. - Map global state (
themeMode) toGLOBAL_APP_STATE. - Map network data (
fetchedUserProfile) toSERVER_CACHE_STATE.
themeMode: GLOBAL_APP_STATE (Context) | isModalOpen: LOCAL_UI_STATE (useState)
categorizeState to classify state variables by scope.
Challenge 3: Compound Component Architecture Simulator
Implement compoundTabsManager(tabs, activeTabId) that coordinates compound tab headers and corresponding active panel bodies dynamically.
- Generate
tabListwithisActive: booleanset. - Find the matching active tab and return its
contentinactivePanel.
Active Tab t1: { tabList: [ { id: "t1", title: "Overview", isActive: true }, ... ], activePanel: "Overview Content..." }