Lesson 9 of 60 – React Project Structure
15%

React Project Structure

Understanding the React project structure is important because a React application contains multiple files and folders, and each one has a specific purpose. In this lesson, you will learn about common folders and files found in a modern React project.

Note: The exact project structure can vary depending on the tools and configuration used to create the React application. The structure shown in this lesson represents a common Vite-based React project.

1. Basic React Project Structure

A newly created React project commonly contains several files and folders.

my-react-app/
│
├── node_modules/
├── public/
├── src/
├── .gitignore
├── index.html
├── package.json
├── package-lock.json
└── vite.config.js

2. Project Root Directory

The main folder of the React application is called the project root directory.

my-react-app/
    ↓
Project Root

Important project files and folders are normally stored inside this directory.

3. node_modules Folder

The node_modules folder contains packages installed for the project and their dependencies.

my-react-app/
 |
 +-- node_modules/
       |
       +-- packages
       +-- dependencies

This folder is normally generated by the package manager rather than being written manually.

4. Why node_modules is Large

A project dependency can itself depend on other packages. As a result, the node_modules folder can contain a large number of files.

React Project
     ↓
Dependency
     ↓
Dependency of Dependency
     ↓
More Packages

5. public Folder

The public folder can contain static assets that need to be served directly.

public/
 |
 +-- images/
 +-- icons/
 +-- other static files

The exact use of this folder depends on the project's build tool and configuration.

6. src Folder

The src folder is where the main application source code is commonly stored.

src/
 |
 +-- App.jsx
 +-- main.jsx
 +-- assets/
 +-- components/

Most React development work happens inside this folder.

7. App.jsx

The App.jsx file commonly contains the main application component.

function App() {

    return (
        <h1>My React App</h1>
    );

}

export default App;

The exact contents of the file depend on the application.

8. main.jsx

The main.jsx file commonly serves as the entry point that connects the React component tree to the HTML page.

import { createRoot } from 'react-dom/client';
import App from './App.jsx';

createRoot(document.getElementById('root'))
    .render(<App />);

9. index.html

The index.html file provides the HTML document used by the application.

<!doctype html>
<html>

<head>
    <title>React App</title>
</head>

<body>

    <div id="root"></div>

</body>

</html>

The React application is mounted into the root element.

10. The root Element

The HTML page commonly contains an element with an ID such as root.

<div id="root"></div>

The React entry file finds this element and renders the React component tree into it.

HTML
 ↓
#root
 ↓
React
 ↓
App Component

11. package.json

The package.json file contains important information about a JavaScript project.

{
    "name": "my-react-app",
    "version": "0.0.0",
    "scripts": {},
    "dependencies": {}
}

It can contain project metadata, scripts, dependencies, and other configuration information.

12. Dependencies

The dependencies section of package.json lists packages required by the application.

{
    "dependencies": {
        "react": "...",
        "react-dom": "..."
    }
}

The exact versions depend on the project setup.

13. devDependencies

Projects can also contain devDependencies. These are packages commonly needed during development or building.

{
    "devDependencies": {
        "vite": "..."
    }
}

The exact packages depend on the project's configuration.

14. package-lock.json

The package-lock.json file records detailed information about installed dependency versions and their dependency tree.

package.json
     ↓
Dependencies
     ↓
package-lock.json
     ↓
Resolved Packages

It helps npm reproduce dependency installations consistently.

15. vite.config.js

In a Vite-based project, vite.config.js can be used to configure Vite.

import { defineConfig } from 'vite';

export default defineConfig({
    // configuration
});

The file may contain project-specific build and development configuration.

16. .gitignore

The .gitignore file tells Git which files and folders should normally not be tracked.

node_modules/
dist/
.env

The exact entries depend on the project and its requirements.

17. assets Folder

A React project can contain an assets folder inside src for files such as images, icons, fonts, or other resources that are imported into the application.

src/
 |
 +-- assets/
       |
       +-- logo.png
       +-- image.jpg

18. components Folder

Developers often create a components folder to organize reusable React components.

src/
 |
 +-- components/
       |
       +-- Header.jsx
       +-- Footer.jsx
       +-- Button.jsx

This is a common organizational pattern rather than a requirement.

19. Creating a Header Component

function Header() {

    return (
        <header>
            <h1>My Website</h1>
        </header>
    );

}

export default Header;

The component can then be imported into another component.

20. Importing a Component

import Header from "./components/Header";

function App() {

    return (
        <div>
            <Header />
        </div>
    );

}

export default App;

This allows the application to use the reusable Header component.

21. Organizing Components

As an application becomes larger, components can be grouped into logical folders.

src/
 |
 +-- components/
 |     +-- Header/
 |     +-- Footer/
 |     +-- Button/
 |
 +-- pages/
 |     +-- Home/
 |     +-- About/
 |
 +-- App.jsx
 +-- main.jsx

22. pages Folder

Some React projects use a pages folder to organize components that represent complete application pages or views.

src/
 |
 +-- pages/
       |
       +-- Home.jsx
       +-- About.jsx
       +-- Contact.jsx

This folder is a common convention, not a mandatory React feature.

23. styles Folder

A project may contain a folder for CSS or other styling files.

src/
 |
 +-- styles/
       |
       +-- App.css
       +-- main.css

The exact organization depends on the styling approach used by the project.

24. Services Folder

Larger applications may use a services folder to keep API-related or external-service logic separate from UI components.

src/
 |
 +-- services/
       |
       +-- api.js
       +-- userService.js

This organization can help separate UI code from application services.

25. Context Folder

Applications using React Context may organize context-related files in a separate folder.

src/
 |
 +-- context/
       |
       +-- AuthContext.jsx
       +-- ThemeContext.jsx

This is an optional project organization pattern.

26. A Larger React Structure

my-react-app/
│
├── public/
│
├── src/
│   ├── assets/
│   ├── components/
│   ├── context/
│   ├── pages/
│   ├── services/
│   ├── styles/
│   ├── App.jsx
│   └── main.jsx
│
├── .gitignore
├── index.html
├── package.json
├── package-lock.json
└── vite.config.js

27. How Files Work Together

index.html
     ↓
main.jsx
     ↓
App.jsx
     ↓
Components
     ↓
Pages
     ↓
Services / Context
     ↓
User Interface

This gives a simplified view of how different parts of a React project can be connected.

28. Development Workflow

Open Project
     ↓
Edit src/
     ↓
Run npm run dev
     ↓
Development Server
     ↓
Browser
     ↓
Test Application
     ↓
Edit Again

29. Project Structure Best Practices

  • Keep related files together.
  • Use meaningful folder names.
  • Separate reusable components.
  • Keep application logic organized.
  • Avoid putting every component in one large file.
  • Use a structure that matches the size of the project.
  • Do not create unnecessary folders for a very small project.
  • Keep configuration files in the project root when appropriate.

30. Complete React Project Structure Summary

A React project is made up of source code, configuration files, dependencies, and static assets. The exact structure can change according to the tools and architecture used by the project.

React Project
     |
     +-- public/
     |     └── Static Files
     |
     +-- src/
     |     +-- components/
     |     +-- pages/
     |     +-- services/
     |     +-- context/
     |     +-- assets/
     |     +-- App.jsx
     |     └── main.jsx
     |
     +-- package.json
     +-- package-lock.json
     +-- index.html
     +-- vite.config.js
     └── .gitignore

Understanding this structure will make it easier to create and organize React applications as you progress through the tutorial.

📌 Key Points

  • The project root contains the main files and folders of the application.
  • The src folder commonly contains React source code.
  • App.jsx commonly contains the main application component.
  • main.jsx commonly connects React to the HTML root element.
  • index.html provides the main HTML document.
  • package.json contains project information, scripts, and dependencies.
  • package-lock.json records resolved dependency information.
  • node_modules contains installed packages.
  • public can contain static assets.
  • components, pages, services, and other folders are common organizational patterns.
  • The exact project structure can vary depending on the tools and application architecture.
  • A well-organized project becomes easier to understand and maintain.

🧠 Quick Quiz

Question: Which folder commonly contains the main React application source code?