Best Practice To Manage Folder Structure For React.js

Search for a command to run...

No comments yet. Be the first to comment.
💡 “So if you want to go fast, if you want to get done quickly, if you want your code to be easy to write, make it easy to read.” -By Robert C. Martin JavaScript has progressed a ton in recent years. If you are learning JavaScript in 2022 and you ...

Before beginning our quest to find how not to use Create React App. First, let’s see why we need Create React App in the first place. Why do we need Create React App? 🤔 Create React App is a comfortable environment for learning React and is the best...

Getting Started with Node.js In this blog, we will try to understand the basics of Node.js, how it works, and why to choose Node.js, but before diving into node let's talk a little bit about Java-script. JavaScript is one of the best programming lang...

Let’s first understand 🤔 What is an Array? Quoting from MDN directly. The Array object, as with arrays in other programming languages, enables storing a collection of multiple items under a single variable name and has members for performing common...


If you are reading this, you probably know what React.js is and might have already used it Earlier. However, you might be wondering why I am reading about folder structure?
Technically you could do that and React.js will take care of everything. But as soon as your project becomes larger, it will become hard for you to maintain your files and debug them. Also if you are going to work in a team with your team members, it's very difficult to grasp your code.
So there must be a perfect folder structure to keep your src folder clean.
React is all about Components!
Components are like JavaScript functions. Every application which we will develop in react will be made up of pieces called components. Components make the task of building UIs much easier.
Actually, entire UIs broke down into multiple individual pieces that are components and then work on them independently and merge them all into a parent component which will be the final UI.
In this section, we will cover only the directory structure of the React.js application.
So let's begin..... 🤩
Open your terminal
React Installation
Run the code
npm i -g create-react-app
npx create-react-app my-app-name
This will create a template React.js app that we can modify to make it our own.
After installing react node_modules will be automatically installed as shown below.

So, first, you need to de-structure it and delete some unwanted files/folders as we won't use all files or sections of code created by the template.
Delete all of these files:
Finally, these files and folders are needed as given below.

Now let me explain the react folder structure which I’m following to develop a new application.

<Routes>
<Route exact path='/' element={< Home />}></Route>
<Route exact path='/about' element={< About />}></Route>
<Route exact path='/contact' element={< Contact />}></Route>
</Routes>
Note: By default, routes are inclusive which means more than one Route component can match the URL path and render at the same time. If we want to render a single component, we need to use Routes.
Since version 6 is completely different from version 5 and uses Routes instead of Switches
I will explain this with the help of an example:- V5 code example:
'./src/app.js'// This is a React Router v5 app
import {
BrowserRouter,
Switch,
Route,
Link,
} from "react-router-dom";
function App() {
return (
<BrowserRouter>
<Switch>
<Route exact path="/">
<Home />
</Route>
<Route path="/about">
<About />
</Route>
</Switch>
</BrowserRouter>
);
}
This is the same app in v6:
'./src/app.js'// This is a React Router v6 app
import {
BrowserRouter,
Routes,
Route,
Link
} from "react-router-dom";
function App() {
return (
<BrowserRouter>
<Routes> // use Routes instead of Switches
<Route path="/"> // Every path is exact in v6
<Home />
</Route>
<Route path="/about">
<About />
</Route>
</Routes>
</BrowserRouter>
);
}
The above example shows the basic structure of react-router.
But if we build a larger application, in that case, we need to follow a basic folder structure.
Let’s suppose you are going to build an application with react and firebase which does everything that’s needed to register, login, and logout users.
Set up everything to create a template Reactjs app.
npm i -g create-react-app
npx create-react-app react-firebase-authentication

You can consolidate all the routes of your application in a well-defined ‘src/constants/routes.js’ constants file shown below.
'src/constants/routes.js'
export const SIGN_UP = '/signup';
export const SIGN_IN = '/signin';
export const HOME = '/home';
export const ACCOUNT = '/account';
export const ADMIN = '/admin';
export const PASSWORD_FORGET = '/pw-forget';
Each route represents a page in your application. For instance, the sign-up page should be reachable in development mode via http://localhost:3000/signup and production mode via http://yourdomain/signup.
'.src/app.js'
import React from 'react';
import { BrowserRouter as Router } from 'react-router-dom';
import Navigation from '../Navigation';
const App = () => (
<Router>
<Navigation />
</Router>
);
export default App;
'.src/components/navigation/index.js'
import React from 'react';
import { Link } from 'react-router-dom';
import * as ROUTES from '../../constants/routes';
const Navigation = () => (
<div>
<ul>
<li>
<Link to={ROUTES.SIGN_IN}>Sign In</Link>
</li>
<li>
<Link to={ROUTES.HOME}>Home</Link>
</li>
<li>
<Link to={ROUTES.ACCOUNT}>Account</Link>
</li>
<li>
<Link to={ROUTES.ADMIN}>Admin</Link>
</li>
</ul>
</div>
);
export default Navigation;
Now, run your application again and verify that the links show up in your browser and the URL also changes.
Notice that even though the URL changes, the displayed content doesn’t change. WHY???
Because here navigation is only there to enable navigation through your application, but no one knows what to render on each route.
Here the route to component mapping plays a role.
How to resolve??
See in our App component, we can specify which components should show up according to corresponding routes with the help of the Route component from React Router.
Open App component
'.src/app.js'
import React from 'react';
import {
BrowserRouter as Router,
Route,
} from 'react-router-dom';
import Navigation from '../Navigation';
import SignUpPage from '../SignUp';
import SignInPage from '../SignIn';
import PasswordForgetPage from '../PasswordForget';
import HomePage from '../Home';
import AccountPage from '../Account';
import AdminPage from '../Admin';
import * as ROUTES from '../../constants/routes';
const App = () => (
<Router>
<div>
<Navigation />
<hr />
<Route path={ROUTES.SIGN_UP} component={SignUpPage} />
<Route path={ROUTES.SIGN_IN} component={SignInPage} />
<Route path={ROUTES.PASSWORD_FORGET} component={PasswordForgetPage} />
<Route path={ROUTES.HOME} component={HomePage} />
<Route path={ROUTES.ACCOUNT} component={AccountPage} />
<Route path={ROUTES.ADMIN} component={AdminPage} />
</div>
</Router>
);
export default App;
Now if a route matches a path prop, the respective component will be displayed and thus, all the page components in the App component are exchangeable by changing the route.
This is how we enable a static frame with various components (e.g. Navigation) around our dynamic pages driven by routes.