Taking a React codebase from an initial MVP to a multi-portal platform requires clear boundaries around folder layout, state ownership, and automated testing so new features do not entangle unrelated screens.
1. Feature-Scoped Directory Layout
Organizing files by domain feature rather than technical file type keeps components, hooks, API clients, and TypeScript contracts co-located:
src/
features/
enrollment/
components/
hooks/
api.ts
types.ts
index.ts
payments/
components/
hooks/
api.ts
types.ts
index.ts
shared/
ui/
lib/
types/
Developers working on payment checkout can trace data flow inside one directory without touching enrollment internals.
2. Keeping State Close to Where It Is Read
Most UI bugs in medium-to-large React apps come from synchronizing state across multiple stores. Separate state into three distinct categories:
- Server cache state: Remote API records, pagination, and background revalidation
- Local UI state: Modal visibility, search inputs, and active tabs kept inside
useState - Shared client stores: Narrow cross-page session preferences managed with lightweight stores like Zustand
import { create } from "zustand";
interface SessionPreferences {
locale: "en" | "fr" | "ru";
setLocale: (locale: "en" | "fr" | "ru") => void;
}
export const usePreferences = create<SessionPreferences>((set) => ({
locale: "en",
setLocale: (locale) => set({ locale }),
}));
3. Compound Components for Accessible UI Primitives
Dialog, tabs, and accordion primitives stay flexible when composed from explicit subcomponents rather than twenty boolean props:
interface DialogProps {
open: boolean;
onClose: () => void;
children: React.ReactNode;
}
export function Dialog({ open, onClose, children }: DialogProps) {
if (!open) return null;
return (
<div role="dialog" aria-modal="true" onClick={onClose}>
<div onClick={(e) => e.stopPropagation()}>{children}</div>
</div>
);
}
4. Custom Hooks for Abortable Data Requests
Encapsulating AbortController cleanup inside data hooks prevents stale responses from overwriting newer filter selections when users type quickly:
import { useEffect, useState } from "react";
export function useJsonResource<T>(url: string) {
const [data, setData] = useState<T | null>(null);
const [loading, setLoading] = useState(true);
const [error, setError] = useState<string | null>(null);
useEffect(() => {
const controller = new AbortController();
setLoading(true);
fetch(url, { signal: controller.signal })
.then((res) => {
if (!res.ok) throw new Error(`HTTP ${res.status}`);
return res.json() as Promise<T>;
})
.then((payload) => {
setData(payload);
setError(null);
})
.catch((err: unknown) => {
if (err instanceof DOMException && err.name === "AbortError") return;
setError(err instanceof Error ? err.message : "Request failed");
})
.finally(() => setLoading(false));
return () => controller.abort();
}, [url]);
return { data, loading, error };
}
5. Testing User Behavior Instead of Implementation Details
Component tests stay resilient across refactors when assertions query accessible roles and labels via React Testing Library and mock network boundaries with MSW v2:
import { render, screen } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import { Counter } from "./Counter";
test("increments count on button activation", async () => {
const user = userEvent.setup();
render(<Counter />);
await user.click(screen.getByRole("button", { name: /increment/i }));
expect(screen.getByText("1")).toBeInTheDocument();
});
Summary
Scalable frontend architecture comes from strict module boundaries, minimal global state, accessible component composition, and tests written from the user's perspective.
