Last updated:
3 min read
How I structure and organize my reactjs components
Rakesh Potnuru
Author
0%
SHARE

How I structure and organize my reactjs components

React is all about components. Better structure helps you to easily debug your app, and understand how the flow is going on, and also as your project grows it becomes easy to manage everything. Now, React doesn't care how you structure your project and components. So in this blog, I am going to share how I structure my project and its components.

Let's get started

Before we start

  • In this article, I don't use any styling framework just plain CSS(sass).
  • Also plain react šŸ˜….

File structure

If you like this structure you can download it from here.

Apart from configurations actual react project starts from the src/ folder. So let's directly jump into it.

ā””ā”€ā”€ src/ ā”œā”€ā”€ app/ ā”œā”€ā”€ assets/ ā”‚ ā””ā”€ā”€ images/ ā”œā”€ā”€ common/ ā”‚ ā”œā”€ā”€ Footer/ ā”‚ ā”‚ ā””ā”€ā”€ components ā”‚ ā”œā”€ā”€ Hooks/ ā”‚ ā”œā”€ā”€ Navigation/ ā”‚ ā”‚ ā””ā”€ā”€ components ā”‚ ā”œā”€ā”€ UIElements/ ā”‚ ā”‚ ā””ā”€ā”€ LoadingAnimations/ ā”‚ ā””ā”€ā”€ Util/ ā”œā”€ā”€ pages/ ā”‚ ā”œā”€ā”€ 404/ ā”‚ ā”œā”€ā”€ About/ ā”‚ ā”‚ ā”œā”€ā”€ components/ ā”‚ ā”‚ ā””ā”€ā”€ About.jsx ā”‚ ā”œā”€ā”€ Contact/ ā”‚ ā”‚ ā”œā”€ā”€ components/ ā”‚ ā”‚ ā””ā”€ā”€ Contact.jsx ā”‚ ā””ā”€ā”€ Home/ ā”‚ ā”œā”€ā”€ components/ ā”‚ ā””ā”€ā”€ Home.jsx ā””ā”€ā”€ styles/ ā”œā”€ā”€ _mixins.scss ā””ā”€ā”€ _variables.scss

I always separate client and server logic even for a small project because it helps to focus on one thing and it makes the project less messy.

Quickly know what each folder contains:

  • app/ - This folder contains actual react app.
  • common/ - This folder contains components that can be used anywhere in the project.
  • pages/ - This folder contains all the common pages of the website.
  • assets/ - All the assets like images, audios etc.
  • styles/ - This folder contains global styles like Sass variables, mixins, functions etc.

Let's dig deep

PinguRobbyTheSealGIF.gif

Pages

. ā””ā”€ā”€ pages/ ā”œā”€ā”€ components/ ā”‚ ā”œā”€ā”€ Hero.jsx ā”‚ ā”œā”€ā”€ Hero.scss ā”‚ ā”œā”€ā”€ CTA.jsx ā”‚ ā””ā”€ā”€ CTS.scss ā””ā”€ā”€ Home.jsx

pages.png

So, here Home.jsx is a route and page. The idea is to imagine each page as a separate HTML file and it contains different sections as components.

Common

. ā””ā”€ā”€ common/ ā”œā”€ā”€ Footer/ ā”œā”€ā”€ Hooks/ ā”œā”€ā”€ Navigation/ ā”œā”€ā”€ UIElements/ ā””ā”€ā”€ Util/

The common folder contains components that are used by other components like loading animations, custom buttons, custom hooks, etc.

App

The app folder contains the main app with all the features like the dashboard, profile page, etc. So you can think app folder as the actual app that unlocks after login.

Component Structure

Now let's take a look at how I structure a component.

Naming

Component name, the file name that contains the component, style sheet file name of the respective component will be the same.

Here's how I structure a component-

component struct.png

For example,

1import react, { useState, useEffect } from 'React'; 2import axios from 'axios'; 3 4import 'CompName.scss'; 5 6const CompName = (props) => { 7 const [visits, setVisits] = useState(0); 8 const [color, setColor] = useState('red'); 9 10 const name = "Cat"; 11 12 useEffect(() => { 13 setVisits(1); 14 }); 15 16 const someFunction = () => { 17 // do something 18 }; 19 20 return ( 21 <div> 22 <h1>Visits: {visits}</h1> 23 </div> 24 ); 25}; 26 27export default CompName;

That's it!


āš’ļø Tool of the week āš’ļø

Checklist design

While designing a website we get confused about what to include on different pages. For example, what all things need to be included on the pricing page? Here comes this website to rescue.

checklist.png


Thanks for reading!


LEAVE A COMMENT OR START A DISCUSSION

MORE ARTICLES

How to compile different languages source code files?

    3 min read

How to compile different languages source code files?

The biggest mistake beginners do while started learning to program is using fancy IDEs (Integrated Development Environment). You should use the terminal at least to know how a spec...

How to host your website on Azure | Step-by-Step Guide

    2 min read

How to host your website on Azure | Step-by-Step Guide

Hosting your website on Azure is very simple. Just a few clicks šŸ‘€....

Subscribe to Newsletter

Weekly

  • Never miss an update.
  • Get articles and snippets directly to your inbox.
  • Subscribe to stay connected and avoid getting lost among millions of websites.

Monthly

Coming soon...

By subscribing, you agree with Revue's Terms of Service and Privacy Policy.