Skip to main content

Command Palette

Search for a command to run...

1.2 Codebase Folder Structure Strategies

Updated
β€’5 min readβ€’View as Markdown

A project's folder structure is a core pillar of your application architecture. If it is weak, your application will eventually suffer from performance bottlenecks, maintenance struggles, duplicate code, and friction within your development team.

While a chaotic structure might work fine for a small learning project with 10 components, an enterprise-level product requires intentional organization. This post breaks down the three primary strategies for structuring your React application, their pros and cons, and when to use each.


1. Why Folder Structure Matters

Setting up a bad folder structure creates a cascading wave of technical debt as your app scales:

  • Difficult Maintenance & Debugging: When bugs show up, you waste time hunting down files across scattered directories instead of jumping straight to the problem.

  • Poor Scalability: A structure that feels fine on day one can become a bottleneck when your app grows from 10 components to hundreds of files.

  • Code Duplication: If developers don't know where a helper function or UI utility lives, they will rewrite it from scratch. This makes global fixes incredibly tedious since the same code exists in 20 different places.

  • Team Friction: Without clear rules, developers will name and place files differently, turning your repository into a confusing maze.


2. The Three Architectural Strategies

Strategy A: Layer-Based Structure

In a layer-based structure, files are grouped strictly by their technical type or role. One folder holds all components, another holds all pages, and a third holds all services.

πŸ“ src
 β”œβ”€β”€ πŸ“ components
 β”‚    β”œβ”€β”€ πŸ“„ Button.jsx
 β”‚    β”œβ”€β”€ πŸ“„ Header.jsx
 β”‚    └── πŸ“„ Sidebar.jsx
 β”œβ”€β”€ πŸ“ pages
 β”‚    β”œβ”€β”€ πŸ“„ Home.jsx
 β”‚    β”œβ”€β”€ πŸ“„ Login.jsx
 β”‚    └── πŸ“„ Profile.jsx
 └── πŸ“ services
      β”œβ”€β”€ πŸ“„ authService.js
      └── πŸ“„ productService.js
  • Pros: Very intuitive for small apps; simple to understand at a glance.

  • Cons: Horrible for large apps. If you have 500 pages or 100 components, searching through a single folder becomes overwhelming.

  • The "Multi-Header" Problem: If your public site needs one Header.jsx and your admin dashboard needs a completely different Header.jsx, keeping them in the same generic folder causes instant naming collisions and confusion.

Strategy B: Feature-Based Structure

A feature-based structure groups files by business modules or user-facing features (like authentication, dashboard, or products) rather than technical layers.

πŸ“ src
 β”œβ”€β”€ πŸ“ auth
 β”‚    β”œβ”€β”€ πŸ“ components
 β”‚    β”‚    └── πŸ“„ LoginForm.jsx
 β”‚    β”œβ”€β”€ πŸ“„ LoginPage.jsx
 β”‚    └── πŸ“„ authService.js
 β”œβ”€β”€ πŸ“ products
 β”‚    β”œβ”€β”€ πŸ“ components
 β”‚    β”‚    └── πŸ“„ ProductCard.jsx
 β”‚    β”œβ”€β”€ πŸ“„ ProductPage.jsx
 β”‚    └── πŸ“„ productService.js
  • Pros: Highly self-contained and modular. If you need to fix a login bug, every related file sits right inside the auth directory.

  • Cons: Falls short when items need to be shared. If the products page and the auth signup page both need the exact same button or user service, it becomes unclear where that shared piece should live.

Strategy C: Hybrid Structure (Industry Standard)

The hybrid structure takes the best parts of both worlds. It modularizes large code blocks by feature but introduces a dedicated shared zone for reusable technical layers.

πŸ“ src
 β”œβ”€β”€ πŸ“ features           (Feature-Based Zone)
 β”‚    β”œβ”€β”€ πŸ“ auth
 β”‚    └── πŸ“ products
 β”œβ”€β”€ πŸ“ shared             (Layer-Based Zone)
 β”‚    β”œβ”€β”€ πŸ“ components    (e.g., Reusable Button, Input)
 β”‚    β”œβ”€β”€ πŸ“ hooks         (e.g., useAuth, useTheme)
 β”‚    └── πŸ“ utils         (e.g., validationHelpers.js)
  • Pros: Highly scalable, clean separation of concerns, and perfectly handles both feature-specific files and global reusability. This is the absolute preferred layout for modern enterprise applications.

3. Best Practices for File Extensions and Rules

To maintain consistency across your team, set up clear rules in a root documentation file (like your README.md or a RULES.md guide):

  • .jsx vs .js Extensions: Use .jsx for any file containing React UI components. For files containing pure JavaScript logicβ€”like network helper wrappers (authService.js) or regex form utilities (validation.js)β€”stick strictly to .js.

  • Casing Clues: Keep folder names lowercase and plural (components, services). Keep UI component files capitalized in PascalCase (AdminLoginForm.jsx). This makes it easy to distinguish a React UI piece from a folder or vanilla utility file at a glance.

  • Service Cohabitation: You don't need a unique file for every single API call. Group related actions into a single fileβ€”like putting login, signup, and passwordReset handlers together inside authService.js. Just avoid mixing unrelated domains, like placing an order configuration inside your authentication service.


πŸ’‘ Interview Q&A (Simple English)

Q1: What is the main difference between Layer-Based and Feature-Based folder structures? Ans: A Layer-Based structure groups files by what they are technically (all components in one folder, all API calls in another). A Feature-Based structure groups files by what they do for the user (all login UI, login pages, and login API calls sit together inside an auth folder).

Q2: Why is a Hybrid folder structure preferred for large enterprise applications? Ans: Because large apps need both feature isolation and code sharing. The Hybrid layout lets you organize distinct modules into feature folders so developers don't get lost, while providing a clear shared folder for universal items like global buttons, custom hooks, or utility functions.

Q3: Why should we use the .js extension for service files instead of .jsx? Ans: The .jsx extension tells React and the build tools that a file contains UI layout code. Service files contain pure JavaScript logic (like API fetch requests) and don't render any UI elements. Keeping them as .js makes it clear that the file is a pure logic utility, not a visual component.

Q4: Is it okay to put multiple API functions inside a single service file? Ans: Yes, absolutely! As long as they are closely related. For example, it makes perfect sense to keep your login, registration, and logout API calls inside a single authService.js file. However, you should never mix unrelated topicsβ€”like putting a product checkout API call into your authentication file.