All posts
architecturefrontend

Organize by Feature, Not by File Type

A components folder with 140 files tells you nothing. Grouping code by feature made our frontend easier to navigate, safer to delete from, and easier to hand to an AI agent.

Muhamad Choirul Adam 3 min readPhoto: Unsplash

Every React project I joined in my first year looked the same from the root: components/, hooks/, services/, utils/, types/. It looks tidy. Then you need to change how commissions are calculated and the code is in five folders, mixed in with everything else.

On the dashboard I work on now, we moved to grouping by feature. It's the single structural change that paid off most.

The shape

src/
  app/                  ← routes only, each file imports a page from a feature
  features/
    orders/
      api/              orders.service.ts → orders.queries.ts → orders.mutations.ts
      components/       OrderTable.tsx, OrderStatusBadge.tsx
      pages/            orders-page.tsx
      schemas/          order-form.schema.ts
      types/
    commissions/
    resellers/
  components/
    ui/                 buttons, inputs, dialogs; no business logic
    shared/             composites used by several features
  lib/                  api client, formatting, nothing feature-specific

Everything about orders lives under features/orders. The route file in app/ is three lines that import the page.

Why it's better

You can find things. "Where's the order status logic?" has one answer, and you can get to it without searching.

Deleting is safe. When we dropped the old voucher feature, it was one folder plus a handful of imports the compiler pointed at. With type-based folders, dead code hides in utils/ forever because nobody is sure what still uses it.

Changes stay small. A pull request for orders touches features/orders. Reviewers know the blast radius from the file list.

The rules that keep it from rotting

Folders are easy. The rules are what make it work:

  1. Features don't import each other's internals. If commissions needs an order, it imports from the orders feature's API layer, not from its components.
  2. components/ui holds no business logic. A button doesn't know what an order is. The moment it does, it belongs to a feature.
  3. Promote late. A component moves to shared/ only when a second feature actually needs it. Not when you think one might.
  4. Each layer has one job. Services make HTTP calls, queries wrap them in React Query, components render. A component never calls the API client directly.
Rendering diagram…

The bonus I didn't expect: AI agents work better here

When I ask an agent to add a filter to the orders table, I can say "the orders feature" and it has everything it needs in one folder: the service, the query keys, the existing table, the form schema. It copies the patterns it finds next door instead of inventing new ones.

In a type-based layout, the same request sends it searching across the whole repo, and it often picks up the wrong pattern from somewhere unrelated. Good structure for humans turns out to be good structure for agents, for the same reason: the context is local.

Where it doesn't fit

For a small app with three screens, this is overkill. A flat structure is fine until you notice yourself opening five folders to make one change. That's the signal.