React Project Structure Walkthrough
After setting up a React project using Vite or Create React App (CRA), understanding the project structure is essential. This helps you organize code, manage components, and maintain a scalable project.
Typical React Project Structure
Hereās a common React project layout:
my-app/
āāā node_modules/ # Installed dependencies
āāā public/ # Static files like index.html, images, icons
āāā src/ # Source code folder
ā āāā assets/ # Images, fonts, icons
ā āāā components/ # Reusable UI components
ā āāā pages/ # Page-level components
ā āāā App.jsx # Main app component
ā āāā index.jsx # Entry point of the React app
ā āāā styles/ # CSS or SCSS files
ā āāā utils/ # Utility/helper functions
āāā package.json # Project metadata and dependencies
āāā vite.config.js # Vite configuration (if using Vite)
āāā README.md # Project documentation
Key Folders and Files Explained
-
public/Contains static files that wonāt be processed by Webpack/Vite.
Example:
index.html, favicon, images. -
src/The heart of your React project. All components, pages, styles, and logic go here.
-
components/Contains reusable UI components like buttons, headers, cards, and modals.
-
pages/Page-level components corresponding to routes in your app. Example: HomePage, AboutPage.
-
App.jsxThe root component that acts as a container for all other components.
-
index.jsxThe entry point where React is rendered into the DOM.
-
styles/Contains CSS or SCSS files for styling the application.
-
utils/Helper functions or utility scripts used across the app, such as API calls or data formatting.
Optional Folders
hooks/ā Custom React hooks for reusable logic.context/ā For global state management using React Context API.services/ā For API requests and external service calls.
Example: index.jsx and App.jsx
index.jsx:
import React from 'react';
import ReactDOM from 'react-dom/client';
import App from './App';
import './styles/global.css';
ReactDOM.createRoot(document.getElementById('root')).render(
<React.StrictMode>
<App />
</React.StrictMode>
);
App.jsx:
import React from 'react';
import Header from './components/Header';
import HomePage from './pages/HomePage';
function App() {
return (
<div>
<Header />
<HomePage />
</div>
);
}
export default App;Ā
