Skip to content

Frontend Architecture Patterns ​

Put boundaries in a large client app so a change lands in one place instead of five.

In this chapter: what architecture buys you · component-based · layered · clean architecture · choosing between them · the mistakes that show up in review

💡 The Core Idea ​

Architecture is the set of rules about which file is allowed to import which other file. That is all it is. Everything else exists to make those rules visible and hard to break by accident. That includes folder names, layer diagrams and dependency-injection containers.

The reason it matters is not elegance. It is the cost of change. In a codebase with no boundaries, a change to the shape of an API response touches components, tests, and three unrelated screens. In a codebase with boundaries, it touches one file. Senior interviews probe this by asking where a change lands, not by asking you to name a pattern.

How It Works ​

Three patterns cover almost every real frontend. They are not alternatives. They stack.

PatternThe rule it addsCost
Component-basedUI is built from independent, typed piecesAlmost none — this is the default
LayeredImports flow one way: UI → logic → dataA folder convention and a lint rule
Clean architectureDomain code imports no framework at allInterfaces and indirection everywhere

Component-based ​

The baseline for React, Svelte, Vue and Angular alike. The one decision worth making is where data fetching lives, because that is what makes a component testable or not.

Split the piece that fetches from the piece that renders:

tsx
interface User {
  id: string;
  name: string;
}

// Presentational — no data fetching, no router, no store. Trivial to test and to reuse.
interface UserCardProps {
  user: User;
  onEdit: () => void;
}

function UserCard({ user, onEdit }: UserCardProps) {
  return (
    <div className="card">
      <h3>{user.name}</h3>
      <button onClick={onEdit}>Edit</button>
    </div>
  );
}

// Container — owns the data and the side effects, renders no markup of its own.
function UserCardContainer({ userId }: { userId: string }) {
  const { data, isLoading } = useUser(userId);
  const router = useRouter();

  if (isLoading || !data) return <Skeleton />;
  return <UserCard user={data} onEdit={() => router.push(`/users/${userId}/edit`)} />;
}

⚠️ In React 19 the container/presentational split is less about testability than it used to be. Server Components already keep fetching out of the interactive tree. The durable principle survives: a component that fetches and a component that renders have different reasons to change.

Layered ​

Three layers, and one rule: imports only ever point downwards.

Imports flow one way; a dotted arrow is a violation a lint rule should reject.

tsx
// Data access — knows about HTTP, knows nothing about React
export const userApi = {
  getById: async (id: string): Promise<User> => {
    const res = await fetch(`/api/users/${id}`);
    if (!res.ok) throw new Error(`User ${id}: ${res.status}`);
    return res.json() as Promise<User>;
  },
};

// Domain — knows about caching and TanStack Query, knows nothing about the URL
export function useUser(id: string) {
  return useQuery({ queryKey: ["user", id], queryFn: () => userApi.getById(id) });
}

// Presentation — knows nothing about either
function UserPage({ id }: { id: string }) {
  const { data } = useUser(id);
  return data ? <UserCard user={data} onEdit={() => {}} /> : <Skeleton />;
}

The layering only holds if something enforces it. In practice that is an ESLint rule (import/no-restricted-paths) or the boundary plugin in your monorepo tool, not a code review habit.

Clean architecture ​

Push the framework to the edge so the domain does not depend on it. In theory, you could replace React with Svelte and leave the core untouched.

A use case the UI calls into:

typescript
// Domain — plain TypeScript, no imports from any framework
interface AuthGateway {
  login(email: string, password: string): Promise<{ token: string }>;
}

export class LoginUseCase {
  constructor(private readonly gateway: AuthGateway) {}

  async execute(email: string, password: string): Promise<User> {
    if (!email.includes("@")) throw new Error("Invalid email");
    const { token } = await this.gateway.login(email, password);
    return decodeUser(token);
  }
}

// Presentation — depends on the use case, which depends on an interface, not on fetch
function LoginPage() {
  const useCase = useMemo(() => new LoginUseCase(new HttpAuthGateway()), []);
  // …
}

This is the pattern teams most often apply too early. It pays for itself in long-lived apps with real domain rules, such as pricing, entitlements or regulated workflows. In a CRUD dashboard it is dead weight.

When to Use It ​

SituationReach forWhy
Any new appComponent-based onlyLayers you do not need are just folders to argue about
Business logic appearing in three componentsLayeredThe duplication is the signal, not the headcount
Domain rules outliving the frameworkClean architectureThe rules survive a rewrite; the components will not
Teams blocking each other on deploysMicro-frontends — see Micro-FrontendsThe bottleneck is organisational, not architectural
Shared code across separate frontendsA monorepo, not a new pattern — see MonoreposTooling solves this cheaper than architecture does

The honest answer in an interview is a sequence, not a pick. Start component-based. Add layering when logic starts repeating. Consider anything heavier only when a concrete pain has a name.

Common Mistakes ​

❌ Business logic inside components. You cannot test a price calculation in a JSX file without rendering it. You cannot reuse it on the server, and it gets copied when a second screen needs it. ✅ Move it to a plain function or a hook the component calls.

❌ Layers with no enforcement. A data/ folder that components import from directly is a naming convention, not an architecture. ✅ Add the lint rule the same day you add the folder.

❌ Clean architecture in a CRUD app. Three interfaces and a use-case class to fetch a list of users is indirection with no payoff. ✅ Introduce it when a domain rule exists that is worth protecting.

❌ Folders by type instead of by feature. With components/, hooks/ and utils/ at 200 files, every feature is smeared across the tree. Nothing can be deleted safely. ✅ Group by feature and keep only genuinely shared code in a shared folder.

🔑 Key Takeaways ​

  • Architecture is a set of import rules. Everything else is a way of showing those rules.
  • Component-based is the correct default, and the only decision it forces is where fetching lives.
  • Layering is worth the cost the moment business logic starts appearing in more than one component.
  • Clean architecture protects domain rules from the framework, and is dead weight without domain rules.
  • A boundary that nothing enforces is a naming convention that will be gone in six months.

Interview Questions ​

Q: How would you structure a frontend that forty engineers work in?

By feature, not by file type, with a small shared layer underneath. Add an enforced rule about which direction imports may point. The reason is deletion: you can remove a feature folder in one commit. A feature spread across components/, hooks/ and utils/ cannot be removed safely at all. Name the enforcement mechanism, such as a lint rule or a build-graph boundary. That makes the answer real, not aspirational.

Q: Where do you put business logic in a React app, and why not in the component?

In plain functions, or in hooks that call them. A component is the wrong home because it can only run inside a render. The logic becomes untestable without a renderer, unusable on the server, and hard to find. To test whether logic belongs in a component, ask whether it would still make sense if the UI were a CLI.

Q: When would you not introduce a layered architecture?

When there is no business logic to layer. A dashboard that fetches and displays is better served by colocated data fetching than by three folders and an indirection per call. Layering is a response to duplication and to logic that outgrew a single screen. Before that point, it costs navigation effort and buys nothing.

Q: A change to one API response shape touched nine files. What went wrong?

The response type leaked past the data-access boundary and became the type the UI renders directly. The fix is a mapping step. The data layer owns the wire shape and maps it to a domain type. Only the domain type may travel upwards. That keeps the next shape change to one file, which is the real point of the boundary.

The whole book, free to read. Generated from the manuscript on every change.

Your progress

Tick each chapter in the sidebar once you have read and understood it. Your name and email keep the ticks on any device.

Your name and email are stored to save your progress. Nothing else is done with them.