React Project Structure
Understand the folders, files, entry point, root component, dependencies, and configuration of a React project created with Vite.
1. What Is React Project Structure?
React project structure refers to the organization of folders and files used to build and manage a React application. Each file has a specific responsibility, such as defining components, adding styles, loading the application, or managing project dependencies.
When we create a React project using Vite, Vite generates an initial folder structure. We can organize additional components, pages, styles, and other files as our application grows.
2. React Project Structure Diagram
This is an example of the common folder structure generated for a Vite React application.
3. Understanding the Project Root
The project root is the main folder of your React application. It contains the source code folder, package information, configuration files, and other project resources.
my-react-app
The main directory where you run npm commands and manage the project.
Important Root-Level Files
package.json
Contains project metadata, dependencies, and
commands such as npm run dev.
package-lock.json
Records the resolved dependency tree used by npm to support consistent installs.
index.html
Provides the HTML document and the root element where the React application is mounted.
vite.config.js
Configures Vite when the project includes and uses this configuration file.
4. Understanding the src Folder
The src folder contains the main application
source code. This is where you will usually create and
maintain React components and their styles.
src
Application Source Code
main.jsx
Starts React rendering
App.jsx
Defines the main UI
App.css
Styles the app component
index.css
Provides global styles
components, pages,
assets, and services inside
src to organize your code.
5. What Is main.jsx?
The main.jsx file is commonly the entry point
of a Vite React application. It imports React-related
functions and the root component, then mounts the
component inside an HTML element.
import { StrictMode } from 'react';
import { createRoot } from 'react-dom/client';
import App from './App.jsx';
import './index.css';
createRoot(document.getElementById('root')).render(
<StrictMode>
<App />
</StrictMode>
);
Code Explanation
StrictMode
Helps identify potential problems during development.
createRoot()
Creates a React root for the target HTML element.
document.getElementById('root')
Finds the HTML element whose ID is
root.
<App />
Renders the application's root component.
6. What Is App.jsx?
The App.jsx file commonly contains the root
application component. It can combine smaller components
to build the overall user interface.
function Header() {
return <h1>CIIT Training Institute</h1>;
}
function CourseList() {
return (
<div>
<p>Java Full Stack</p>
<p>React Development</p>
<p>Python Full Stack</p>
</div>
);
}
function App() {
return (
<div>
<Header />
<CourseList />
</div>
);
}
export default App;
Expected Browser Output
CIIT Training Institute
Java Full Stack
React Development
Python Full Stack
7. What Is index.html?
The index.html file is the HTML entry document
served by Vite. In a typical Vite React template, it contains
a root element and a script that loads main.jsx.
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>My React App</title>
</head>
<body>
<div id="root"></div>
<script type="module" src="/src/main.jsx"></script>
</body>
</html>
id="root" value must match the element
selected in main.jsx. This is where the React
tree is mounted in this example.
8. What Is package.json?
The package.json file stores project metadata,
scripts, and dependency declarations. npm uses the scripts
defined here when you run commands such as
npm run dev.
{
"name": "my-react-app",
"version": "0.0.0",
"private": true,
"type": "module",
"scripts": {
"dev": "vite",
"build": "vite build",
"preview": "vite preview"
},
"dependencies": {
"react": "project-installed-version",
"react-dom": "project-installed-version"
},
"devDependencies": {
"vite": "project-installed-version"
}
}
package.json. Keep the actual version ranges
and dependencies generated by your project.
Important npm Commands
npm run dev
Starts the development server.
npm run build
Builds the application for production.
npm run preview
Previews the production build locally when it exists.
9. Understanding public and node_modules
public
Stores static files that need to be served as-is, such as a favicon or other publicly accessible assets.
- Favicon files
- Static images
- Other public resources
node_modules
Contains installed packages used by the project and its development tools.
- React packages
- Vite and other dependencies
- Packages installed by npm
node_modules.
npm manages this folder. It is generally excluded from Git
using .gitignore.
10. Organizing a React Application
As your React project becomes larger, you can create additional
folders inside src. The following is an example
of a more organized application structure.
components
Stores reusable UI pieces such as Header and CourseCard.
pages
Stores larger components representing application pages.
assets
Stores assets imported and processed by the application.
services
Can contain reusable API requests and data-related functions.
11. How the Main Files Work Together
index.html
Provides the root HTML element and loads the entry script.
main.jsx
Creates a React root and renders the App component.
App.jsx
Defines the main UI and composes child components.
Browser UI
The user sees the rendered application.
12. Practice Task
Task: Explore Your Vite React Project
- Open your project folder in Visual Studio Code.
- Find the
srcfolder. - Open and read
main.jsx. - Open
App.jsxand identify the root component. - Open
index.htmland locate the root element. - Open
package.jsonand check its scripts. - Create a
componentsfolder insidesrc. - Explain the role of each important file in your own words.
Summary
- The project root contains source code and project configuration files.
- The
srcfolder contains the main application source code. main.jsxcommonly serves as the React entry point.App.jsxcommonly defines the root component.index.htmlprovides the HTML root element.package.jsonstores project scripts and dependencies.node_modulescontains installed packages.- Additional folders help organize larger applications.