How to create generic component in ReactJS
Getting Started
If you're new to programming, you might be wondering what a component is. Think of a component as a Lego brick. In the same way that you can use Lego bricks to build a variety of structures, you can use components to build a variety of features in your application. A 'generic' component is like a Lego brick that you can customize — you get to decide its shape, color, and function.
In ReactJS, we use components to build our application's user interface. But when we have similar components with different data, we don't want to create a new component for each one. Instead, we make our components generic so we can reuse them. This is not only efficient, but it also makes our code cleaner and easier to maintain.
Creating a Generic Component
Let's say we're building a website for a library. We need a component to display information about each book. Each book has a title, author, and a cover image. Here's how we create a generic book component.
function Book({ title, author, coverImage }) {
return (
<div>
<img src={coverImage} alt={title} />
<h2>{title}</h2>
<p>{author}</p>
</div>
);
}
In this example, Book
is a functional component that takes in props
(properties). props
are like parameters to a function. They let us pass data into our component. The curly braces {}
let us destructure props
so that we can use title
, author
, and coverImage
directly.
We can now use our Book
component to display information about different books. Here's how we might do that:
<Book
title="To Kill a Mockingbird"
author="Harper Lee"
coverImage="mockingbird.jpg"
/>
<Book
title="1984"
author="George Orwell"
coverImage="1984.jpg"
/>
In these examples, we're using our Book
component like a customized Lego brick. We're giving it different shapes (titles), colors (authors), and functions (cover images).
Advanced Usage: Children Prop
Sometimes, we want our generic component to include other components or elements. In ReactJS, we can achieve this using the children
prop.
Suppose we want to add a button inside our Book
component that lets users add the book to their reading list. Here's how we can update our Book
component to accept children
:
function Book({ title, author, coverImage, children }) {
return (
<div>
<img src={coverImage} alt={title} />
<h2>{title}</h2>
<p>{author}</p>
{children}
</div>
);
}
Now, we can include any elements or components we want inside our Book
component, like this:
<Book
title="To Kill a Mockingbird"
author="Harper Lee"
coverImage="mockingbird.jpg"
>
<button>Add to reading list</button>
</Book>
In this example, the button
element is passed as a children
prop to the Book
component. This allows us to customize our Book
component even further.
Conclusion
Creating generic components in ReactJS is like having a magic Lego set. You can create a single brick and customize it to fit your needs, instead of creating a new brick for each different situation. This magic set not only makes your work easier but also transforms your application into a clean, well-organized, and maintainable structure.
Whenever you find yourself writing similar components, remember the magic Lego set. Can you make your components generic? Can you reuse your components with different data or functionality? If the answer is yes, then congratulations! You're practicing efficient, effective, and elegant coding.
So, go ahead. Create, reuse, and let your codebase bloom with generic components.