React Folder Structure - Complete Guide for Beginners to Advanced
By Saket Bhatnagarβ’β’Beginner to Advanced
React Folder Structure
- 1When we create a React project using tools like Create React App (CRA) or Vite, it automatically generates a folder structure for us.
- 2This structure helps us organize our code properly and keeps everything clean and manageable as the project grows.
- 3π¦ Create React App (CRA) Folder Structure:
- node_modules/ β This folder contains all the packages (libraries) that your project depends on. It is auto-managed by npm or yarn.
- public/ β This folder stores static files like
index.html
, images, and the site favicon. The browser can directly access files from here. - src/ β This is where we write all our React code. It includes components, styles, logic, and assets used in the application.
- src/index.js β This is the starting point of the app. It renders the
App
component into the DOM. - src/App.js β This is the root component where we structure our UI and import other components.
- package.json β This file keeps track of dependencies, scripts (like start or build), and basic project information.
- 4
Example: CRA Folder Structure
1my-app/2 βββ node_modules/3 βββ public/4 β βββ index.html5 βββ src/6 β βββ App.js7 β βββ index.js8 βββ package.json - 5β‘ Vite Project Folder Structure:
- index.html β In Vite, this file is placed directly in the root folder (not inside
public/
). It serves as the template for the app. - src/ β Just like CRA, all main logic, components, and styles are written inside the
src
folder. - main.jsx or main.tsx β This is the entry point for Vite apps, similar to
index.js
in CRA. - vite.config.js β This is the configuration file for Vite. We can use it to define custom behavior, add plugins, or configure path aliases.
- index.html β In Vite, this file is placed directly in the root folder (not inside
- 6
Example: Vite Folder Structure
1vite-project/2 βββ node_modules/3 βββ src/4 β βββ App.jsx5 β βββ main.jsx6 βββ index.html7 βββ package.json8 βββ vite.config.js - 7π Important Points for Interviews:
- In CRA, the
index.html
is inside thepublic
folder, but in Vite, it's placed directly in the root. - Both tools use a
src/
folder where we write our React code. - In CRA, the entry point is
index.js
. In Vite, itβsmain.jsx
. - CRA uses Webpack under the hood, while Vite is faster and uses ESBuild.
- Viteβs config file
vite.config.js
is used to customize project setup, similar to Webpack configs.
- In CRA, the
- 8π‘ Why do we need this structure?
- It separates logic from static assets like HTML and images.
- It keeps our codebase organized and scalable.
- It makes it easier for teams to collaborate and understand the project quickly.
- 9By understanding the folder structure, we can easily add new components, manage assets, and keep the app clean and professional.
- 11π’ Production-Level Folder Structure in React Projects (for experienced developers):
- Once your project grows (team members, features, API integrations), it's important to organize the code for real-world maintainability.
- A common pattern is called Feature-based folder structure or Modular architecture.
- 12
Example: Production Folder Structure
1my-app/2βββ public/3βββ src/4β βββ assets/ # Images, icons, logos, fonts, etc.5β βββ components/ # Reusable UI components (Button, Modal, Card, etc.)6β βββ features/ # Feature-based folders (e.g. auth, dashboard, posts)7β β βββ auth/8β β β βββ AuthPage.jsx9β β β βββ authSlice.js10β β β βββ authAPI.js11β β βββ posts/12β β βββ PostList.jsx13β β βββ postSlice.js14β β βββ postAPI.js15β βββ hooks/ # Custom hooks like useAuth, useFetch, etc.16β βββ pages/ # Route-level components (used in react-router)17β βββ layouts/ # AppShells or wrappers like Sidebar + Main Layout18β βββ services/ # API logic (axios configs, endpoints)19β βββ store/ # Redux store or Zustand/Context state management20β βββ utils/ # Helper functions and utilities21β βββ App.jsx # Root component with router setup22β βββ main.jsx # Entry point (same as index.js in CRA)23βββ .env # Environment variables24βββ package.json25βββ vite.config.js26βββ README.md - 13π Folder Descriptions:
- components/ β Contains common UI pieces reused across different features (like Button, Input, Modal).
- features/ β Grouped by business logic or domain (e.g., auth, posts, profile). Keeps feature code self-contained.
- hooks/ β Custom React hooks to avoid repetition. Example:
useDebounce
,useScroll
. - pages/ β Used with routing, each file becomes a route like
/login
,/dashboard
. - layouts/ β Shared layout components like Sidebars, Headers, or TabNavigations.
- services/ β All API call logic, usually grouped by resource (authService, postService).
- store/ β Central state management (Redux, Context API, Zustand) setup and files.
- utils/ β Helper functions like dateFormat, currencyFormatter, validators, etc.
- assets/ β Static files like images, icons, logos, CSS files.
- 14β
Why This Structure is Better for Large Apps?
- Helps team members quickly find and understand code.
- Makes each feature isolated and easier to test/debug.
- Improves code reuse and modularity.
- Prepares your app to scale as features grow.
- 15π£ Interview Points:
- In real-world apps, we follow modular folder structure based on features or domains.
- We separate components, hooks, services, pages, and state logic to improve maintainability.
- React Router pages go in
pages/
, reusable UI goes incomponents/
. - API calls and logic are usually kept in
services/
or insidefeatures/
folders.
- 16With this production-ready structure, you can confidently scale the app, add teams, and keep everything organized!