Learn React, React Tutorial

Reusable Components in React

In modern web development, building reusable UI components plays a crucial role in crafting scalable and easily maintainable applications. React Hooks has...

Written by Shivangi Rajde · 4 min read >
reusable components

In modern web development, building reusable UI components plays a crucial role in crafting scalable and easily maintainable applications. React Hooks has revolutionized this process, making it even more streamlined and efficient. In this comprehensive guide, we’ll dive deep into the world of React Hooks and explore the best practices for creating reusable UI components.

Throughout this article, we’ll uncover the power of React Hooks, utilizing tools like useState for efficient state management and implementing useEffect to handle side effects seamlessly. By the end of our journey together, you’ll unlock the full potential of React Hooks and master the art of developing reusable UI components that not only boost productivity but also elevate the overall code quality of your projects.

So, let’s embark on this exciting exploration and harness the capabilities of React Hooks to create dynamic and maintainable UI components that will enhance your web development endeavors.

Setting up a new project

To begin a new project, we’ll go through the process of setting up React.js using Create React App. For detailed instructions on how to do this, you can refer to the following link: “https://www.letsreact.org/how-to-install-react-js-with-create-react-app/”. This link provides a step-by-step guide on setting up your new project with all the necessary information and insights you need to get started with React.js development.

npx create-react-app react-reusable-demo

When should we use reusable components?

Using reusable components in web development is a best practice that offers several advantages. Reusable components allow developers to create a modular, scalable, and maintainable codebase. Here are some scenarios when it’s beneficial to use reusable components:

  1. Repeated Elements: When you have UI elements or functionality that appear multiple times across your application, it’s a clear indication that you should create a reusable component. Examples could be buttons, forms, navigation bars, alerts, etc.
  2. Consistency: Reusable components help enforce consistency in the user interface. By using the same components throughout the application, you ensure a uniform look and feel, enhancing the user experience.
  3. Simplifying Development: Developing applications becomes more efficient when you can leverage existing components rather than recreating the same logic and styles from scratch every time.
  4. Maintainability: Reusable components are easier to maintain. When a change or bug fix is required, updating a single component will reflect the changes everywhere it’s used.
  5. Scalability: As your application grows, having a library of reusable components allows you to scale your project without sacrificing code quality.
  6. Team Collaboration: Reusable components promote collaboration among team members. Developers can work on different parts of the application simultaneously, focusing on building components independently and integrating them seamlessly.
  7. Third-party Integration: If your application interacts with third-party libraries or APIs, encapsulating their functionality within reusable components makes it easier to manage integrations and updates.
  8. Testing: Reusable components can be individually unit-tested, ensuring that their behavior remains consistent across the application.
  9. Code Readability: Well-structured, reusable components enhance code readability, making it easier for other developers (or your future self) to understand and work with the codebase.
  10. UI Libraries: In larger projects or within a team or organization, building a UI library of reusable components can standardize design patterns and UI elements across multiple projects.

Reusable components offer significant advantages in terms of code organization, maintainability, and scalability. When you identify patterns or UI elements that appear more than once in your application, consider creating reusable components to optimize your development process and improve the overall quality of your project.

Using reusable components

To build a simple, reusable React component, we can start by following these steps:

Step 1: Create a new file for your component, for example, “GenericComponent.js.”

Step 2: Copy the provided code into the “GenericComponent.js” file.

import React from "react";

export default function GenericComponent(props) {
  return (
    <div>
      <span>GenericComponent</span>
      <br />
      <input type="text" onChange={props.onChange} />
      <div style={{ paddingTop: "10px", paddingBottom: "10px" }}>
        <button onClick={props.onClick}>Show Text</button>
      </div>
    </div>
  );
}

Step 3: Import React at the top of the file, as it is required to use JSX.

You have a generic version of the “GenericComponent” that can be reused across different parts of your application.

Generic component in react
Generic component

To use this component in another file, follow these steps:

Step 1: Import the “GenericComponent” in the file where you want to use it.

Step 2: Define the “handleChange” and “handleClick” functions in the parent component and pass them as props to the “GenericComponent.”

Step 3: You can now use the “GenericComponent” with the custom event handlers in the parent component.

To demonstrate we have two files that use the GenericComponent, have  a look at the category component

import React from "react";
import GenericComponent from './GenericComponent';

export default function Category() {
  const [catState, setCatState] = useState({});
  const [shouldShowText, setShouldShowText] = useState(false);

  useEffect(() => {
    setShouldShowText(false);
  }, []);

  const catButtonClicked = () => {
    console.log('Category Button Clicked');
    setShouldShowText(true);
  };

  const catInputChange = (event) => {
    console.log('Category on change');
    const { name, value } = event.target;
    setCatState({ ...catState, name: value });
    setShouldShowText(false);
  };

  return (
    <div>
      <h3>Category</h3>
      <GenericComponent
        handleClick={catButtonClicked}
        handleChange={catInputChange}
      />
      <span>{shouldShowText && catState.name}</span>
    </div>
  );
}

By following these steps, you’ve successfully built a simple and reusable React component that can be used across your application, allowing different parent components to customize its behavior based on their specific requirements.

Using generic component
Output of using generic component

Best practices for creating reusable components

Creating reusable components is essential for building scalable and maintainable React applications. To ensure you’re making the most of this approach, here are some best practices to follow when developing reusable components:

  1. Single Responsibility Principle: Aim to give each component a single responsibility or purpose. A component that does too much becomes harder to reuse and maintain.
  2. Props and Composition: Use props to make your components customizable and versatile. Allow users of your components to pass data and behavior as props to tailor the component’s functionality to their specific needs. Additionally, embrace composition by breaking down complex components into smaller, more manageable ones, which can be easily combined to create more intricate UIs.
  3. Immutability: Avoid directly mutating props or state within a component. Instead, follow the principles of immutability to create predictable and reliable components.
  4. Default Props and Prop Types: Define default props and use prop types to specify the expected types of props. This practice helps prevent runtime errors and provides better documentation for your components.
  5. Avoiding Component Specificity: While building reusable components, focus on generalizing them to serve various use cases. Components that are too specific might limit their reusability.
  6. Testing: Write unit tests for your components to ensure they behave as expected and maintain consistent functionality across different scenarios.
  7. Separate Data and UI Logic: Whenever possible, keep your data manipulation and UI logic separate. This separation of concerns makes your components more flexible and adaptable.
  8. Context Usage (with Caution): Use React Context sparingly and only for data that genuinely affects a significant part of your application. Overusing context can lead to complex relationships and reduced component reusability.
  9. Document Your Components: Provide clear and concise documentation for your reusable components, including their props, usage examples, and any specific considerations.
  10. Follow Accessibility Guidelines: Ensure your components are accessible to all users, adhering to accessibility guidelines and best practices.
  11. Versioning: If you’re creating a shared component library, consider implementing versioning to manage changes and updates in a controlled manner.
  12. Consistent Styling: If your components include styling, try to maintain consistent styles across all components. Following a design system or style guide can help achieve this.
  13. Performance Considerations: Be mindful of performance when building reusable components. Optimize rendering, avoid unnecessary re-renders, and implement shouldComponentUpdate or React.memo when appropriate.

By incorporating these best practices into your development workflow, you can create highly effective and widely applicable reusable components that enhance productivity, maintainability, and overall code quality in your React projects.

Some related articles
How to Create Toast Notifications in React with React Toastify?
Package.json Explained
Creating reusable components in React
How to use Bootstrap with React?

Loading

Leave a Reply

Your email address will not be published. Required fields are marked *