React Project Structure Best Practices for Scalable Application
Tags: javascript, react, webdev, productivity
Published: 2020-06-09 12:10:09

This post was originally published at <a href="https://www.devaradise.com/react-project-folder-structure">https://www.devaradise.com/react-project-folder-structure</a>
<!-- wp:paragraph -->
<p>A react project structure or architecture plays an important role in project maintenance. A good folder structure will help developers in the team easy to locate and easy to relate (<strong>ELER</strong>).</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>React ecosystem give freedom to developers to structure react project. We can structure it as we like.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>However, if you're just starting a medium-to-large scale react project, it can be confusing to structure the project properly so that your team can easy to maintain it later.</p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2>So, How to Structure your React Project?</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>I know, there's a lot of articles out there talking about this in different approaches. To be honest, for me there is no best project architecture that can fit with any projects and programmer coding styles.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>What I will show you here is simply an opinionated way to structure a react project. You can adopt some parts or all of them for your project. That's your choice.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>So, here is my best react folder structure for scalable applications. I will explain each folder function and the reason why I include that.</p>
<!-- /wp:paragraph -->

<!-- wp:image {"align":"center","id":2211,"sizeSlug":"large","linkDestination":"media"} -->
<div class="wp-block-image"><figure class="aligncenter size-large"><a href="https://www.devaradise.com/wp-content/uploads/2020/06/react-project-structure-1.jpg"><img src="https://www.devaradise.com/wp-content/uploads/2020/06/react-project-structure-1.jpg" alt="" class="wp-image-2211"/></a><figcaption>React folder structure</figcaption></figure></div>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>Here is the explanation..</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":3} -->
<h3>1. Assets folder</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>The assets folder contains images and scss folder for styling. In this project architecture, I am using centralized styling with SCSS files. I am more comfortable with that.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>If you prefer to include style in each component, that's not a problem. But, you should consider locating a default or shared styling here. </p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>You can rename it to css or styles if you don't use scss/sass in your project.</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":3} -->
<h3>2. Components folder</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>The components folder contains a collection of UI components like button, custom input field, modal, etc that will be shared and used across files in the project.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>You can rename it to 'elements' or 'UIs' if you dont like 'components'.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Each component has a test file to help us maintain them because they used widely in the project.</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":3} -->
<h3>3. Pages folder</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>The pages folder reflects the routes of the application. Each component inside this folder has its own route. </p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>A page component will contain children from components folder, parts folder, or its own subfolder. It has its own state, and usually call some services as well.</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":3} -->
<h3>4. Parts</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>The parts folder is almost the same as components folder. It contains a reusable components that used in the pages.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>The difference is that components in parts folder reflect parts of a page, like footer, sidebar, and header, while the components folder contains standalone UI components like button, form, or input field.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Sometime, A component in parts can use some components from the components folder.</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":3} -->
<h3>5. Services</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>The services folder is inspired by Angular architecture (well, I am an Angular developer too).</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>In <a href="https://www.devaradise.com/best-angular-dashboard-templates">Angular</a> there is a feature called Dependency Injection that allows us to inject a service anywhere in a project. Most of the time, a service used to manage API integrations. So, it separated from logic in the component.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Service is not a react component. It simply a javascript function to manage API integration based on the type of data.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>To be clear, here is how i write a service in most of my react projects.</p>
<!-- /wp:paragraph -->

<!-- wp:image {"id":2229,"sizeSlug":"large","linkDestination":"media"} -->
<figure class="wp-block-image size-large"><a href="https://www.devaradise.com/wp-content/uploads/2020/06/writing-service.png"><img src="https://www.devaradise.com/wp-content/uploads/2020/06/writing-service-1024x823.png" alt="Coding a service" class="wp-image-2229"/></a></figure>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>And, here is how i call it in components.</p>
<!-- /wp:paragraph -->

<!-- wp:image {"id":2228,"sizeSlug":"large","linkDestination":"media"} -->
<figure class="wp-block-image size-large"><a href="https://www.devaradise.com/wp-content/uploads/2020/06/calling-service.png"><img src="https://www.devaradise.com/wp-content/uploads/2020/06/calling-service-1024x791.png" alt="Calling a service" class="wp-image-2228"/></a></figure>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>I am using axios for API calling. To call services in component, I use javascript promise.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>You may ask, why bother separate API calling in service. We can just call API with Axios inside components. </p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Well, what if we need to call API in different components, by different developers?</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>I bet, the codes won't be standardized, and it can be difficult to maintain if the developers change.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>By using services, we can solve this problem by centralizing API calling based on data type. If someone want to use it, just call it with promise in the components.</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":3} -->
<h3>6. Store (If using Redux)</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>The store folder will be included if you decide to use redux in your project. Inside it, there are actions and reducers subfolder to manage redux states.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Mostly, the actions and reducers will be called in the page components so they usually named based on pages that use them.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>If your application is a small-to-medium scale project, try to maximize the usage of the props and states component before using redux. If that's too complicated, well, you can use redux.</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":3} -->
<h3>7. Utils</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>The utils folder is just a place to locate some utility functions that used repeatedly in the project. Files in the utils folder should only contain some functions like date formatting, string conversion, etc.</p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2>Do we need to include all the folders from the beginning?</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Well, No. You don't have to include all folders from the beginning of the project. </p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>For instance, If you're not sure to use redux or not in your project, you don't have to create a store folder yet.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Services folder is also not necessary if you sure that your project only consumes a small number of APIs.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>However, the assets, components, parts, and pages folders are important to be arranged from the beginning to manage better codes.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>That's it. What do you think? If you have an opinion, please feel free to comment and suggest your ideas so we can have more thoughts about this.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Happy coding!</p>
<!-- /wp:paragraph -->