file and folder architecture in react js

muhammedshamal

Muhammed Shamal PV

Posted on August 2, 2024

file and folder architecture in react js

Follow me on LinkedIn
Follow me on Github.com

Click & Read

Creating a well-structured file and folder architecture is crucial for maintaining, scaling, and collaborating on React projects. Here is a comprehensive guide to setting up a professional file and folder structure for your React applications, suitable for sharing with the dev community in a blog post.

Introduction

When working on React projects, having a consistent and logical file structure can make a significant difference in code readability and maintainability. This guide will cover a modular approach to organizing your files and folders, promoting best practices that can be scaled for larger projects.

Top-Level Folder Structure

A typical React project setup includes the following top-level directories and files:

my-react-app/
├── public/
├── src/
│   ├── assets/
│   ├── components/
│   ├── hooks/
│   ├── pages/
│   ├── services/
│   ├── store/
│   ├── styles/
│   ├── utils/
│   ├── App.js
│   ├── index.js
│   └── routes.js
├── .env
├── .gitignore
├── package.json
├── README.md
└── node_modules/
Enter fullscreen mode Exit fullscreen mode

Folder Descriptions

1. public/

This folder contains static assets that you want to be served directly by the server.

  • index.html: The main HTML file.
  • favicon.ico: The favicon for your app.

2. src/

This is where all the source code for your application lives.

a. assets/

This folder is for images, fonts, and other static assets.

src/
└── assets/
    ├── images/
    ├── fonts/
    └── icons/
Enter fullscreen mode Exit fullscreen mode
b. components/

Reusable UI components should be placed here. Each component gets its own folder.

src/
└── components/
    ├── Button/
    │   ├── Button.js
    │   ├── Button.test.js
    │   └── Button.css
    ├── Header/
    │   ├── Header.js
    │   ├── Header.test.js
    │   └── Header.css
    └── ...
Enter fullscreen mode Exit fullscreen mode
c. hooks/

Custom hooks are placed here.

src/
└── hooks/
    ├── useAuth.js
    ├── useFetch.js
    └── ...
Enter fullscreen mode Exit fullscreen mode
d. pages/

Each page or route in your application has its own folder. Pages can also contain sub-components that are specific to that page.

src/
└── pages/
    ├── Home/
    │   ├── Home.js
    │   ├── Home.css
    │   └── components/
    │       └── HomeFeature.js
    ├── About/
    │   ├── About.js
    │   ├── About.css
    │   └── components/
    │       └── Team.js
    └── ...
Enter fullscreen mode Exit fullscreen mode
e. services/

This folder is for API calls and other external services.

src/
└── services/
    ├── api.js
    └── auth.js
Enter fullscreen mode Exit fullscreen mode
f. store/

If you are using Redux or any state management library, put your store configuration and slices/reducers here.

src/
└── store/
    ├── index.js
    ├── userSlice.js
    └── productSlice.js
Enter fullscreen mode Exit fullscreen mode
g. styles/

Global CSS, SASS, or styled-components can be placed here.

src/
└── styles/
    ├── variables.scss
    ├── mixins.scss
    └── global.css
Enter fullscreen mode Exit fullscreen mode
h. utils/

Utility functions and helpers.

src/
└── utils/
    ├── formatDate.js
    ├── calculateDiscount.js
    └── ...
Enter fullscreen mode Exit fullscreen mode

3. Core Files

App.js

The root component that wraps all other components.

import React from 'react';
import { BrowserRouter as Router } from 'react-router-dom';
import Routes from './routes';

function App() {
  return (
    <Router>
      <Routes />
    </Router>
  );
}

export default App;
Enter fullscreen mode Exit fullscreen mode
index.js

The entry point of the application.

import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';
import './styles/global.css';

ReactDOM.render(
  <React.StrictMode>
    <App />
  </React.StrictMode>,
  document.getElementById('root')
);
Enter fullscreen mode Exit fullscreen mode
routes.js

Defines the routes for the application.

import React from 'react';
import { Route, Switch } from 'react-router-dom';
import Home from './pages/Home/Home';
import About from './pages/About/About';

function Routes() {
  return (
    <Switch>
      <Route path="/" exact component={Home} />
      <Route path="/about" component={About} />
    </Switch>
  );
}

export default Routes;
Enter fullscreen mode Exit fullscreen mode

Conclusion

Organizing your React project with a clear, modular file structure can make your codebase more maintainable and scalable. This structure helps in keeping related files together, promoting reusability and separation of concerns.

Alway do this like file & folder structuring in any tech

Happy Coding!

💖 💪 🙅 🚩
muhammedshamal
Muhammed Shamal PV

Posted on August 2, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related