Dev.to WebDev 🛠 Dev 👁 0 📖 3 min read

How I Structure Large React + Next.js Apps (A Kanpur Developer's Playbook)

I'm Mohd Zakir Hassan, a Tech Lead and React developer in Kanpur. Over 6+ years I've seen React apps stay pleasant for years and I've seen them turn into a maze in six months. The difference is rarely the framework; it's

I'm Mohd Zakir Hassan, a Tech Lead and React developer in Kanpur. Over 6+ years I've seen React apps stay pleasant for years and I've seen them turn into a maze in six months. The difference is rarely the framework; it's structure. This is the playbook I follow for large React and Next.js apps.

1. Organise by feature, not by file type

A folder per file type (components, hooks, utils) works until the app has fifty screens. Then every change touches five folders. I group by feature instead:

src/
  app/                 # Next.js routes: thin
  features/
    billing/
      components/
      hooks/
      api.ts
      index.ts         # the feature's public API
    auth/
    dashboard/
  shared/              # truly generic UI and helpers

When someone asks "where does billing live?", the answer is one folder.

2. Keep Next.js routes thin

Files under app/ should mostly compose: fetch what the page needs, then render feature components. If a route file grows business logic, it is hard to test and hard to reuse. I treat routes as wiring, and keep the logic in features/.

3. Separate server state from UI state

Data that comes from an API is server state; whether a modal is open is UI state. Mixing them in one global store is the most common source of confusing bugs. In Next.js, server components can fetch data directly. For client-side data I use a query library such as React Query, and plain component state for UI. Most apps never need a global store beyond that.

4. Give every feature a public API

Each feature exports only what others may use through its index.ts. Other features import from features/billing, never from features/billing/components/InvoiceRow. I enforce this with a lint rule so it does not depend on memory. This one habit is what keeps a large codebase refactorable, because you can change a feature's internals without breaking its consumers.

5. Validate at the boundary

Anything crossing a boundary (API responses, form input, route params) gets validated once, with a schema library such as Zod, and typed from there on. TypeScript only protects code you control; the network is not that.

6. Measure before you optimise

I use the React Profiler and real user metrics before touching performance. Usually a few heavy re-renders or an oversized bundle explain most of the slowness. Dynamic imports for heavy, rarely used components and the Next.js image component cover a lot of ground before anything exotic is needed.

7. Secure by default

Short-lived JWTs, refresh tokens in httpOnly cookies, and role checks on the server. Next.js middleware is good for redirects, but authorisation decisions belong next to the data, not only in the UI.

8. Make code review the architecture meeting

As a tech lead in Kanpur, the biggest lever I have is review. I keep pull requests small, comment on structure (boundaries, naming, where logic lives) more than style, and write down decisions that surprised us so the next person doesn't relitigate them.

9. Test the seams, not the pixels

I write most tests at feature boundaries: component tests against each feature's public surface, plus a handful of end-to-end tests for the journeys that must never break, such as signing in or completing a payment. Tests inside a feature can change freely when its internals change. Tests at the public API should rarely change, and when they do, it tells you a boundary has moved. I mock the network at the edge, with a tool like MSW, so tests exercise real component code instead of mocked hooks.

10. Agree conventions once

Naming, file layout and when to extract shared code are worth deciding early. I follow a rule of three: duplicate once, and extract the third time, when real use cases show what the abstraction should be. Premature shared components become the most-changed and least-understood files in the repo. Keep the rules in a short CONTRIBUTING file; a one-page doc that people read beats a wiki nobody opens. Watch the shared/ folder too, because it quietly turns into a dumping ground. If only one feature uses something, move it back into that feature.

Start small

You don't need to adopt all of this on day one. Start with feature folders and thin routes; the rest follows when the app needs it. If you're building AI features into your React app, I wrote about building AI agents with Node.js too, and you can find more about my background on my page for software developer in Kanpur.

Originally published at zakirhassan.github.io. I'm Mohd Zakir Hassan, a software developer and tech lead in Kanpur — more about me here.

📰 Read the original article on Dev.to WebDev

Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.