WEEKLY REACT RUNDOWN

How I Structure and What Tools I Use to Build High-Quality React Apps

A Subjective But Powerful Guide

Leandro πŸ€– πŸ‘Ύ πŸš€
4 min readJul 26, 2024
Photo by Alireza Attari on Unsplash

Introduction

As technology development progresses exponentially, we must constantly study new trends and learn new tools. Regardless, I advocate reviewing the foundations of computer science and engineering. After all, today’s code will be legacy tomorrow.

In this article, we will discuss the importance of structuring a software application according to good practices such as DRY, SOLID, KISS, and more.

Project Structure

Overview of Project Structure

I can’t stress enough how important it is to start simple, but a well-organized project structure and evolve it when needed; even though this is very subjective, you need to have a methodology! In the same way, a developer does not need to be concerned with optimization at the beginning of the project but always keep in mind that the day will come.

Folder Structure

src/                 # Root folder for source code
β”œβ”€β”€ assets/ # Static assets such as images, fonts, etc.
β”œβ”€β”€ components/ # Contains Atomic Design layers
β”‚ β”œβ”€β”€ atoms/ # Basic building blocks of the UI
β”‚ β”œβ”€β”€ molecules/ # Combinations of atoms
β”‚ β”œβ”€β”€ organisms/ # More complex components made of molecules
β”‚ β”œβ”€β”€ templates/ # Page layouts combining organisms
β”‚
β”œβ”€β”€ contexts/ # Context API for dependency injection and state mgmt
β”œβ”€β”€ hooks/ # Custom hooks for reusable logic
β”œβ”€β”€ services/ # API calls and service functions
β”œβ”€β”€ store/ # Redux or other state management stores
β”œβ”€β”€ styles/ # CSS/SCSS files or styled-components
β”œβ”€β”€ utils/ # Utility functions and helpers
└── pages/ # Specific pages using templates

Now, let's delve into how you could structure a component like a Button:

src/
β”œβ”€β”€ components/
β”‚ β”œβ”€β”€ atoms/
β”‚ β”‚ β”œβ”€β”€ Button/
β”‚ β”‚ β”‚ β”œβ”€β”€ Button.jsx # Component implementation
β”‚ β”‚ β”‚ β”œβ”€β”€ Button.module.scss # Component-specific styles
β”‚ β”‚ β”‚ β”œβ”€β”€ Button.stories.jsx # Storybook stories
β”‚ β”‚ β”‚ └── Button.test.jsx # Component tests
β”‚ β”‚ └── ...
β”‚ └── ...

The folder encapsulates all the Button related file, ensuring that each component is self-contained, with its implementation, styles, stories, and tests. This promotes modularity and makes it easier to manage and scale your components.

Principles for High-Quality Code

DRY: Don’t Repeat Yourself

The DRY principle emphasizes the importance of reducing repetition in your codebase. It makes your code more maintainable and easier to refactor.

  • Reusable Components: Create components that can be used across different parts of your application.
  • Custom Hooks: Encapsulate reusable logic in custom hooks.
  • Utils: Provide common utility functions and helpers.
  • ESLint: A powerful tool for identifying and enforcing coding standards, including detecting duplicate code segments.

SOLID Principles

  • Single Responsibility Principle (SRP)
  • Open-Closed Principle (OCP)
  • Liskov Substitution Principle (LSP)
  • Interface Segregation Principle (ISP)
  • Dependency Inversion Principle (DIP)

I wrote a couple of articles with a real-life example here and here.

KISS: Keep It Simple, Stupid

The KISS principle emphasizes simplicity in design and implementation. Simple code is easier to read, understand, and maintain.

β€œAny fool can write code that a computer can understand. Good programmers write code that humans can understand.” β€” Martin Fowler

Atomic Design

Atomic Design is a methodology for creating design systems by breaking down UI components into five distinct levels: Atoms, Molecules, Organisms, Templates, and Pages. This approach promotes reusability, scalability, and consistency in your component libraryEssential Tools for React Development. I leave the Pages out of the Components folder since they are more aligned with routing and top-level views

Benefits of Structured Approach

Maintainability

  • Easier Bug Fixes and Enhancements: With a well-organized structure, finding and fixing bugs is easier. Adding new features or updating existing ones is also simpler because the code less coupled and logically arranged.
  • Consistent Codebase: A standardized structure ensures that all developers follow the same patterns and practices, which reduces the cognitive load and helps maintain consistency across the codebase.

Readability

  • Clear Organization: A clean and consistent folder structure makes it easier for developers to understand the project’s architecture and flow. This is especially beneficial for new team members during their onboarding.

Scalability

  • Modular Components: By following principles like Atomic Design, components are built in a modular fashion, making it easier to scale the application as new features are added.
  • Future-Proofing: A well-structured project is more adaptable to change. Whether you need to integrate new technologies or modify existing ones, a clear structure helps ensure these changes can be made without disrupting the entire codebase.

Conclusion

In this article, we’ve explored the essential principles and tools that contribute to building high-quality React applications. By structuring your project effectively and adhering to core software engineering principles like DRY, SOLID, and KISS, you can ensure that your codebase remains maintainable, readable, and scalable. Additionally, adopting the Atomic Design methodology helps create a modular and reusable component system, enhancing the overall efficiency and consistency of your development process.

Adopting these practices and tools in your React projects can significantly improve the quality and maintainability of your applications. By investing time in structuring your project I can guarantee that it will pay off!

Like what you see?

--

--

Leandro πŸ€– πŸ‘Ύ πŸš€

A Tech adventurer aiming to explore and share the latest advancements in technology. Do you want to join my guild and level up with me?