Skip to content

balajidharma/react-folder-structures

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

6 Commits
Β 
Β 

Repository files navigation

πŸ“‚ React Application Folder Structures: A Comprehensive Guide

A detailed exploration and comparison of the most effective and scalable folder structures for modern React applications. Choosing the right architecture is crucial for team velocity, maintainability, and long-term project health.

🌟 Motivation: Why Structure Matters

As a React application grows past a dozen components, the location of files directly impacts how quickly new features can be added and how easily bugs can be fixed.

A good folder structure helps development teams by:

  • Improving Discoverability: Quickly find where a specific piece of logic (a component, a hook, a service) resides.

  • Enforcing Consistency: Ensures all team members follow the same organisation pattern.

  • Enhancing Scalability: Allows the app to grow without becoming a tangled mess of imports.

  • Facilitating Refactoring: Makes it easier to safely move or delete features with minimal ripple effects.

πŸš€ React Structure Patterns

We analyze three primary patterns used in the industry, detailing the pros and cons of each.

πŸ—‚οΈ Component-Based Structure

A component-based (type-based) folder structure in React organises files based on their type or role within the application, promoting modularity and reusability.

src/
β”œβ”€β”€ components/
β”‚   β”œβ”€β”€ Button/
β”‚   β”‚   β”œβ”€β”€ Button.tsx             # The primary component file
β”‚   β”‚   β”œβ”€β”€ Button.css             # Local styles (or .module.css, .scss)
β”‚   β”‚   β”œβ”€β”€ Button.test.tsx        # Unit tests
β”‚   β”‚   β”œβ”€β”€ index.ts               # Exporting the component (facade)
β”‚   β”‚   └── types.ts               # TypeScript interfaces/props
β”‚   β”œβ”€β”€ Card/
β”‚   β”‚   β”œβ”€β”€ Card.tsx
β”‚   β”‚   β”œβ”€β”€ Card.css
β”‚   β”‚   β”œβ”€β”€ index.ts
β”‚   β”‚   └── types.ts
β”‚   β”œβ”€β”€ index.ts                   # Main export file for the *entire* library
β”‚   └── shared-types.ts            # Global types used across components
β”œβ”€β”€ hooks/                         # Any reusable hooks
β”œβ”€β”€ utils/                         # Any reusable utilities
└── index.ts                       # Entry point for build tools (e.g., Webpack/Rollup)

πŸ—‚οΈ Component-Based Structure for project

src/
β”œβ”€β”€ components/           // Reusable, generic components used throughout the application
β”‚   β”œβ”€β”€ Button/
β”‚   β”‚   β”œβ”€β”€ Button.jsx
β”‚   β”‚   └── Button.module.css
β”‚   β”œβ”€β”€ Card/
β”‚   β”‚   β”œβ”€β”€ Card.jsx
β”‚   β”‚   └── Card.module.css
β”‚   └── ...
β”œβ”€β”€ layout/               // Components specifically for application layout
β”‚   β”œβ”€β”€ MainLayout/
β”‚   β”‚   β”œβ”€β”€ MainLayout.jsx
β”‚   β”‚   └── MainLayout.module.css
β”‚   β”œβ”€β”€ Header/
β”‚   β”‚   β”œβ”€β”€ Header.jsx
β”‚   β”‚   └── Header.module.css
β”‚   β”œβ”€β”€ Footer/
β”‚   β”‚   β”œβ”€β”€ Footer.jsx
β”‚   β”‚   └── Footer.module.css
β”‚   └── ...
β”œβ”€β”€ menu/                 // Components specifically for navigation menus
β”‚   β”œβ”€β”€ MainMenu/
β”‚   β”‚   β”œβ”€β”€ MainMenu.jsx
β”‚   β”‚   └── MainMenu.module.css
β”‚   β”œβ”€β”€ UserMenu/
β”‚   β”‚   β”œβ”€β”€ UserMenu.jsx
β”‚   β”‚   └── UserMenu.module.css
β”‚   └── ...
β”œβ”€β”€ pages/                // Top-level components representing distinct application pages/views
β”‚   β”œβ”€β”€ HomePage/
β”‚   β”‚   β”œβ”€β”€ HomePage.jsx
β”‚   β”‚   └── HomePage.module.css
β”‚   β”œβ”€β”€ AboutPage/
β”‚   β”‚   β”œβ”€β”€ AboutPage.jsx
β”‚   β”‚   └── AboutPage.module.css
β”‚   └── ...
β”œβ”€β”€ hooks/                // Custom React hooks
β”‚   β”œβ”€β”€ useAuth.js
β”‚   └── useDebounce.js
β”œβ”€β”€ utils/                // Utility functions
β”‚   β”œβ”€β”€ api.js
β”‚   └── helpers.js
β”œβ”€β”€ assets/               // Static assets (images, fonts, etc.)
β”‚   β”œβ”€β”€ images/
β”‚   └── fonts/
β”œβ”€β”€ App.jsx               // Main application component
β”œβ”€β”€ index.js              // Entry point of the application
└── styles/               // Global styles or theme variables
    β”œβ”€β”€ global.css
    └── variables.css

πŸ“Œ Directory Overview

components/

Houses small, independent, and reusable UI components that can be used across different parts of the application (e.g., Button, Card, Input)

layouts/

Contains components responsible for the overall structure and arrangement of content on a page. This includes components like MainLayout (which might wrap the entire application content), Header, and Footer.

pages/

Represents distinct views or screens of the application. These components often compose other smaller components to form a complete page (e.g., HomePage, ProductPage).

hooks/

Stores custom React hooks for encapsulating reusable logic.

App.jsx

The root component that defines routing and layout selection.

index.js

Entry point of the React application where the React root is created and the app is mounted.

βœ… Pros (Type-Based)

  • Simple to Start: Easy for small projects and new developers to understand.

  • Clear Technical Separation: If you know you're looking for a component, you go to components/.

❌ Cons (Type-Based)

  • Poor Scalability: As the app grows, the components/ folder can become a massive, unmanageable list of hundreds of files.

  • Feature Dispersal: Files related to a single feature (e.g., "User Profile") are spread across multiple top-level folders (components/ProfileButton, hooks/useProfileData, services/profileAPI).

πŸ—‚οΈ Feature-Based Structure

This approach groups all files related to a specific feature or domain into one self-contained folder. This is the recommended modern approach for medium-to-large applications.

src/
β”œβ”€β”€ features/
β”‚   β”œβ”€β”€ Auth/
β”‚   β”‚   β”œβ”€β”€ components/
β”‚   β”‚   β”‚   β”œβ”€β”€ LoginForm.jsx
β”‚   β”‚   β”‚   └── RegisterForm.jsx
β”‚   β”‚   β”œβ”€β”€ hooks/
β”‚   β”‚   β”‚   └── useAuth.js
β”‚   β”‚   └── pages/
β”‚   β”‚       └── LoginPage.jsx
β”‚   β”œβ”€β”€ Dashboard/
β”‚   β”‚   β”œβ”€β”€ components/
β”‚   β”‚   β”‚   └── Widget.jsx
β”‚   β”‚   └── pages/
β”‚   β”‚       └── DashboardPage.jsx
β”‚   └── ... (other features)
β”œβ”€β”€ layouts/
β”‚   β”œβ”€β”€ MainLayout.jsx
β”‚   β”œβ”€β”€ AdminLayout.jsx
β”‚   └── components/
β”‚       β”œβ”€β”€ Header.jsx
β”‚       β”œβ”€β”€ Sidebar.jsx
β”‚       β”œβ”€β”€ Footer.jsx
β”‚       └── Menu.jsx
β”œβ”€β”€ shared/
β”‚   β”œβ”€β”€ components/
β”‚   β”‚   β”œβ”€β”€ Button.jsx
β”‚   β”‚   └── Modal.jsx
β”‚   β”œβ”€β”€ hooks/
β”‚   β”‚   └── useDebounce.js
β”‚   └── utils/
β”‚       └── helpers.js
β”œβ”€β”€ App.jsx
β”œβ”€β”€ index.js

πŸ“Œ Directory Overview

features/

Contains isolated feature modules. Each module may include:

  • Components
  • Hooks
  • Pages
  • API logic
  • Context or state management
layouts/

Houses global layout components that define structural UI for different app sections.

  • MainLayout.jsx β€” used for general/public sections
  • AdminLayout.jsx β€” used for admin-only sections
  • layouts/components/ β€” structural UI elements shared across layouts (Header, Sidebar, Menu, etc.)
shared/

Contains fully reusable, truly generic utilities and UI elements designed to be used anywhere in the app.

App.jsx

The root component that defines routing and layout selection.

index.js

Entry point of the React application where the React root is created and the app is mounted.

βœ… Pros (Feature-Based)

  • High Cohesion (Feature Concentration): Everything you need for a feature is in one folder. Deleting a feature often means just deleting one directory.

  • Reduced Scope Creep: Developers only need to worry about the files within their current feature directory.

  • Excellent Scalability: Adding a new feature simply means creating a new top-level folder under features/.

❌ Cons (Feature-Based)

  • Initial Overhead: Can feel overly complex for very small projects.

  • Defining "Shared" vs. "Feature-Specific": Sometimes tricky to decide if a component belongs in a feature's subfolder or the global shared/components folder.

🀝 Contributing

Contributions are what make the open-source community an amazing place to learn, inspire, and create. Any contributions you make are greatly appreciated.

  1. Fork the Project.

  2. Create your Feature Branch (git checkout -b feature/AmazingFeature).

  3. Commit your Changes (git commit -m 'Add some AmazingFeature').

  4. Push to the Branch (git push origin feature/AmazingFeature).

  5. Open a Pull Request.

πŸ“œ License

Distributed under the MIT License. See LICENSE for more information.

Releases

No releases published

Packages

No packages published