Elevating React Development: A Guide to Generic Components

meghaghotkar

Megha Ghotkar

Posted on February 28, 2024

Elevating React Development: A Guide to Generic Components

In the world of React development, creating reusable and adaptable components is key to building scalable and maintainable applications. Generic components, in particular, offer a versatile solution for encapsulating common UI patterns and functionalities. In this blog post, we'll explore how to implement and utilize generic components in React, along with code examples demonstrating their effectiveness.

Understanding Generic Components

Generic components, also referred to as reusable components or UI primitives, are modular building blocks designed to be reused across different parts of an application or even in multiple projects. These components are typically abstracted to encompass common UI patterns, behaviors, and functionalities.

Implementing Generic Components in React

Let's dive into a practical example by creating a generic component in React. This component will encapsulate common button functionalities and allow for customization through props.

import React, { FC, ReactNode, MouseEvent } from 'react';
interface ButtonProps {
  onClick: (event: MouseEvent<HTMLButtonElement>) => void;
  color?: string;
  variant?: 'solid' | 'outline';
  children: ReactNode;
}

const Button: FC<ButtonProps> = ({ onClick, color = '#007bff', variant = 'solid', children }) => {
  const buttonStyles = {
    backgroundColor: color,
    padding: '10px 20px',
    borderRadius: '5px',
    border: 'none',
    cursor: 'pointer',
    color: variant === 'outline' ? color : '#fff',
    outline: 'none',
  };

  return (
    <button style={buttonStyles} onClick={onClick}>
      {children}
    </button>
  );
};

export default Button;



Enter fullscreen mode Exit fullscreen mode

In the above example
We've defined the ButtonProps interface to specify the prop types.
The Button component is defined as a functional component (FC) accepting the ButtonProps interface as its props.
We've provided default values for the color and variant props using TypeScript's optional property syntax (?).
TypeScript automatically infers the prop types based on the interface, so we don't need to explicitly specify prop types as in PropTypes.

Utilizing Generic Components

Now, let's see how we can use our component in different parts of our application:

import React, { MouseEvent } from 'react';
import Button from './Button';
const App: React.FC = () => {
  const handleClick = () => {
    alert('Button clicked!');
  };

  return (
    <div>
      <h1>Using Generic Components in React</h1>
      <Button onClick={handleClick}>Click me</Button>
      <Button onClick={handleClick} color="green" variant="outline">
        Submit
      </Button>
    </div>
  );
};

export default App;
Enter fullscreen mode Exit fullscreen mode

In this above example
The App component is defined as a functional component (FC) with no props.
We import MouseEvent from react to specify the type of the handleClick function parameter.
We import the Button component from its relative path.
We use the Button component with different props (onClick, color, variant) as in the original code.

Conclusion

Generic components are a powerful tool for building reusable and maintainable React applications. By abstracting common UI patterns and functionalities into generic components, developers can streamline development, promote consistency, and enhance code maintainability.
Through practical examples like the component demonstrated above, you can leverage the flexibility and reusability of generic components to create scalable and adaptable React applications.

💖 💪 🙅 🚩
meghaghotkar
Megha Ghotkar

Posted on February 28, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related