How I structure my react components

How I structure my react components

ยท

3 min read

React is all about components. Better structure helps you to easily debug your app, 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 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/

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 like 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,

import react, { useState, useEffect } from 'React';
import axios from 'axios';

import 'CompName.scss';

const CompName = (props) => {
  const [visits, setVisits] = useState(0);
  const [color, setColor] = useState('red');

  const name = "Cat";

  useEffect(() => {
    setVisits(1);
  });

  const someFunction = () => {
    // do something
  };

  return (
    <div>
      <h1>Visits: {visits}</h1>
    </div>
  );
};

export 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, follow for more ๐Ÿ˜‡.

Did you find this article valuable?

Support itsrakesh by becoming a sponsor. Any amount is appreciated!

ย