cod;nncode. learn. thrive.

SOLID Principles in React

Posted Mar 02, 2023

how you can implement SOLID principles in ReactJS with practical example

SOLID is a set of principles that can help developers write clean, maintainable, and extensible code. These principles were first introduced by Robert C. Martin, also known as Uncle Bob, and they have since become a standard for object-oriented design.

SOLID stands for:

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

In this article, we'll explore how you can implement SOLID principles in ReactJS.

Single Responsibility Principle (SRP)

The Single Responsibility Principle states that a class should have only one reason to change. In ReactJS, this means that each component should have a single responsibility, such as rendering a UI element, managing state, or handling user input.

For example, Let's say you have a component that displays a list of items and fetches the data from an API. To apply the SRP principle, you can separate the data fetching logic into a separate service or hook:

// ItemList.js

import { useEffect, useState } from "react";
import { fetchItems } from "./itemService";

function ItemList() {
  const [items, setItems] = useState([]);

  useEffect(() => {
    fetchItems().then((data) => setItems(data));
  }, []);

  return (
    <ul>
      {items.map((item) => (
        <li key={item.id}>{item.name}</li>
      ))}
    </ul>
  );
}

export default ItemList;
// itemService.js

export async function fetchItems() {
  const response = await fetch("https://api.example.com/items");
  const data = await response.json();
  return data;
}

In this example, the ItemList component only focuses on rendering the list of items and does not handle the data fetching logic. The fetchItems function is responsible for fetching the data from the API and can be reused by other components or services.

Open/Closed Principle (OCP)

The Open/Closed Principle states that a class should be open for extension but closed for modification. In ReactJS, this means that you should be able to add new features or components to your application without modifying the existing code.

Let's say you have a form component that validates user input. To apply the OCP principle, you can make the validation logic extensible by using a higher-order component:

// withValidation.js

import { useState } from "react";

function withValidation(WrappedComponent) {
  return function ValidatedComponent(props) {
    const [errors, setErrors] = useState({});

    function validateInput(name, value) {
      // Add validation rules here
    }

    function handleChange(event) {
      const { name, value } = event.target;
      validateInput(name, value);
    }

    function handleSubmit(event) {
      event.preventDefault();
      // Add form submission logic here
    }

    return (
      <WrappedComponent
        {...props}
        errors={errors}
        onChange={handleChange}
        onSubmit={handleSubmit}
      />
    );
  };
}

export default withValidation;
// Form.js

import withValidation from "./withValidation";

function Form({ errors, onChange, onSubmit }) {
  return (
    <form onSubmit={onSubmit}>
      <input type="text" name="username" onChange={onChange} />
      {errors.username && <span>{errors.username}</span>}
      <input type="password" name="password" onChange={onChange} />
      {errors.password && <span>{errors.password}</span>}
      <button type="submit">Submit</button>
    </form>
  );
}

export default withValidation(Form);

In this example, the withValidation higher-order component provides the validation logic and passes it to the wrapped Form component as props. This way, you can add new validation rules or use different validation strategies by creating new higher-order components.

Liskov Substitution Principle (LSP)

The Liskov Substitution Principle states that subtypes should be substitutable for their base types. In ReactJS, this means that you should be able to use a child component in place of its parent component without affecting the behavior of the application.

Let's say you have a parent component that expects a child component to render a list of items. To apply the LSP principle, you can make the child component substitutable by using the same props as the parent component:

// Parent.js

function Parent({ children }) {
  return <div>{children}</div>;
}
// Child.js

function Child({ items }) {
  return (
    <ul>
      {items.map((item) => (
        <li key={item.id}>{item.name}</li>
      ))}
    </ul>
  );
}

export default Child;

In this example, the Child component expects an array of items as props and renders a list of items. The Parent component can accept any child component that renders a list of items, regardless of its implementation details. This way, you can easily swap the Child component with another component that satisfies the same props interface without affecting the Parent component.

Interface Segregation Principle (ISP)

The Interface Segregation Principle states that a client should not be forced to depend on methods it does not use. In ReactJS, this means that you should avoid creating components that have too many props or methods that are not used by the client.

Let's say you have a component that displays a user profile with a picture, name, and bio. To apply the ISP principle, you can split the component into smaller components that focus on specific aspects of the profile:

// Avatar.js

function Avatar({ src, alt }) {
  return <img src={src} alt={alt} />;
}

export default Avatar;
// Name.js

function Name({ name }) {
  return <h1>{name}</h1>;
}

export default Name;
// Bio.js

function Bio({ bio }) {
  return <p>{bio}</p>;
}

export default Bio;
// Profile.js

import Avatar from "./Avatar";
import Name from "./Name";
import Bio from "./Bio";

function Profile({ user }) {
  return (
    <>
      <Avatar src={user.avatar} alt={user.name} />
      <Name name={user.name} />
      <Bio bio={user.bio} />
    </>
  );
}

export default Profile;

In this example, the Profile component is split into smaller components that each focus on a specific aspect of the profile. This way, you can reuse the Avatar, Name, and Bio components in other parts of the application without including unnecessary code.

Dependency Inversion Principle (DIP)

The Dependency Inversion Principle states that high-level modules should not depend on low-level modules, but both should depend on abstractions. In ReactJS, this means that you should avoid coupling components to specific implementations of dependencies, such as data sources, APIs, or libraries.

Let's say you have a component that fetches data from an API and displays it. To apply the DIP principle, you can use a dependency injection library like InversifyJS to invert the dependency between the component and the data source:

// DataService.js

import { injectable } from "inversify";

@injectable()
class DataService {
  async fetchData() {
    const response = await fetch("https://api.example.com/data");
    const data = await response.json();
    return data;
  }
}

export default DataService;
// DataComponent.js

import { useEffect, useState } from "react";
import { inject } from "inversify";
import { TYPES } from "./types";

function DataComponent({ dataService }) {
  const [data, setData] = useState([]);

  useEffect(() => {
    dataService.fetchData().then((data) => setData(data));
  }, [dataService]);

  return (
    <ul>
      {data.map((item) => (
        <li key={item.id}>{item.name}</li>
      ))}
    </ul>
  );
}

export default inject(TYPES.DataService)(DataComponent);
// types.js

export const TYPES = {
  DataService: Symbol("DataService"),
};

In this example, the DataService class is responsible for fetching data from the API and can be injected into the DataComponent using the inject function from InversifyJS. This way, you can easily swap the DataService implementation with another implementation that satisfies the same interface without affecting the Data Component.

Conclusion

In summary, the SOLID principles provide guidelines for writing maintainable and extensible code. By following these principles, you can improve the architecture and design of your ReactJS applications, leading to better code quality and easier maintenance. It's important to note that while these principles can be applied to ReactJS, they can also be applied to other programming languages and frameworks.

As a final note, it's worth mentioning that the SOLID principles are not a silver bullet for all programming problems. There may be cases where following the principles could lead to over-engineering or unnecessary complexity. It's important to use your best judgment and apply the principles where they make sense for your specific use case.

Further Resources

Git How to Stash

The `git stash` command is used to stash changes in the working directory. This command saves changes in the stash stack, which can later be applied or popped.

Read Here

CRUD Operations in ReactJS Without API: GitHub Code Step-by-Step Example 2024

This article dives into implementing CRUD operations specifically in ReactJS without relying on an external API, providing a comprehensive step-by-step guide and a GitHub code example.

Read Here

Table Pagination tutorial in Reactjs using Server Side API data with Paginate, CSS example

Master the art of React pagination! Learn React.js pageable and paginate, style it with CSS, fetch data from APIs, and even implement server-side pagination with Node.js. Explore practical examples and level up your web development skills today.

Read Here

JavaScript Object Creation: Mastering Classes and Prototypes for Efficiency

Explore different methods of creating objects in JavaScript, with a focus on the 'class' keyword. Learn when to use each approach and the benefits they offer.

Read Here

Learn how to create two simple responsive side navigation bar and main navbar in ReactJs

Learn how to create a simple two responsive side navigation bar and navbar in React JS to enhance user experience and create a more interactive web application.

Read Here
Your feedback is our favorite notification! Share your thoughts about this page and make us smile.