Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

Sorry, you do not have permission to ask a question, You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please type your username.

Please type your E-Mail.

Please choose an appropriate title for the post.

Please choose the appropriate section so your post can be easily searched.

Please choose suitable Keywords Ex: post, video.

Browse

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Navigation

  • Home
  • About Us
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • About Us
  • Contact Us
Home/ Questions/Q 5511

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Latest Questions

Author
  • 61k
Author
Asked: November 27, 20242024-11-27T09:57:07+00:00 2024-11-27T09:57:07+00:00

How I structure my react components

  • 61k

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 
Enter fullscreen mode Exit fullscreen mode

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 
Enter fullscreen mode Exit fullscreen mode

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/ 
Enter fullscreen mode Exit fullscreen mode

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; 
Enter fullscreen mode Exit fullscreen mode

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 πŸ˜‡.

beginnersreactwebdev
  • 0 0 Answers
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question

Stats

  • Questions 4k
  • Answers 0
  • Best Answers 0
  • Users 2k
  • Popular
  • Answers
  • Author

    ES6 - A beginners guide - Template Literals

    • 0 Answers
  • Author

    Understanding Higher Order Functions in JavaScript.

    • 0 Answers
  • Author

    Build a custom video chat app with Daily and Vue.js

    • 0 Answers

Top Members

Samantha Carter

Samantha Carter

  • 0 Questions
  • 20 Points
Begginer
Ella Lewis

Ella Lewis

  • 0 Questions
  • 20 Points
Begginer
Isaac Anderson

Isaac Anderson

  • 0 Questions
  • 20 Points
Begginer

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help

Footer

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore, ask, and connect. Join our vibrant Q&A community today!

About Us

  • About Us
  • Contact Us
  • All Users

Legal Stuff

  • Terms of Use
  • Privacy Policy
  • Cookie Policy

Help

  • Knowledge Base
  • Support

Follow

© 2022 Querify Question. All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.