Back to the academy

Clean frontend architecture in React (Zustand + Presenters + custom routing/i18n)

How to avoid monolithic components and prop drilling: domain folders, scoped Zustand stores and JSON internationalisation without heavy libraries.

Intermediate · React · Zustand · TypeScript · Vite

Folders by domain, not by file type

A `components/` folder with two hundred files says nothing about the product. Organising by feature —`live-trading/`, `courses/`, `about/`— keeps each feature's component, test, styles and helpers in one place. Inside, subfolders are named after what they are (`lesson/`, `data/`, `store/`) and leaf components stay flat, with their test next to them. This very academy is organised that way.

❌ Antipattern

src/
├─ components/   (212 files)
├─ hooks/        (64 files)
├─ utils/        (97 files)
└─ tests/        (a mirror of everything)

✅ Stoic pattern

src/modules/platform/pages/web/courses/
├─ CoursesPage.tsx
├─ CoursesPage.test.tsx
├─ coursesPresenter.ts
├─ data/coursesCatalog.ts
└─ lesson/
   ├─ CourseLessonPage.tsx
   └─ CourseLessonPage.test.tsx

Key takeaways

  • The folder structure should tell what the product does, not which file types it uses.
  • The test lives next to the code it tests: it moves and is deleted with it.
  • No code file goes past 300 lines: when it grows, split it by responsibility.

Zustand stores scoped per feature, with no prop drilling

State shared by several components of the same feature lives in a Zustand store inside that feature, not in a global store or passed down five levels of props. Each subcomponent reads only what it needs through a selector, which also avoids needless renders. A global store is reserved for data that genuinely crosses modules.

❌ Antipattern

<Page prices={prices} setPrices={setPrices} symbol={symbol}>
  <Panel prices={prices} setPrices={setPrices} symbol={symbol}>
    <Row prices={prices} symbol={symbol} />
  </Panel>
</Page>

✅ Stoic pattern

// live-trading/hooks/useLiveTradingStore.ts
export const useLiveTradingStore = create<LiveTradingState>()(set => ({
  pricesBySymbol: {},
  setPrices: prices => set({ pricesBySymbol: prices }),
}));

// any component of the feature reads only what it renders
const price = useLiveTradingStore(s => s.pricesBySymbol[symbol]);

Key takeaways

  • A feature's shared state: one store inside that feature.
  • Narrow selectors: every component subscribes only to what it renders.
  • React Context only for short-lived state of one subtree, such as pagination or a toggle.

Custom routing and i18n without heavy libraries

On a site with few top-level routes, a map of views kept in sync with `pushState` and `popstate` replaces a full router, and one JSON file per language with a `translate` function and `{name}` parameters replaces an i18n library. Fewer dependencies mean less weight and less surface to keep updated. The discipline comes from tests: every key must exist in every language and no visible string can be hand-written in the JSX.

❌ Antipattern

<button>{t('courses.start') || 'Empezar curso'}</button>
<p>Precio: {price}€</p>

✅ Stoic pattern

// es.json: "courses.minutes": "{minutes} min de lectura y práctica"
<button>{translate('courses.start')}</button>
<p>{translate('courses.minutes', { minutes: course.minutes })}</p>

Key takeaways

  • Before adding a dependency, check whether forty lines of your own solve the case.
  • Never a fallback like `t('key') || 'text'`: it hides the keys missing in other languages.
  • A language-parity test turns a forgotten translation into a visible failure.