Building Reusable React Components for Scalable Frontend Development
Ever feel like you’re stuck copy-pasting the same components endlessly? It’s time to break the cycle with reusable React components —…
Building Reusable React Components for Scalable Frontend Development
Ever feel like you’re stuck copy-pasting the same components endlessly? It’s time to break the cycle with reusable React components — keeping your code clean, efficient, and DRY (Don’t Repeat Yourself), just like a well-loved recipe you don’t need to reinvent every time!
1. The “Button” Component That Loves to Party 🥳
Let’s say you have this super cool button in your app, but it’s always changing. Sometimes it’s blue, sometimes it’s red, and occasionally it even throws in some confetti when clicked! But why re-write that party button for every occasion when you can just invite it over and over with reusable components?
import React from "react";
const PartyButton = ({ color, onClick, children }) => {
return (
<button
onClick={onClick}
style={{ backgroundColor: color, padding: "10px 20px", borderRadius: "5px" }}
>
{children}
</button>
);
};
const App = () => {
const handleClick = () => alert("🎉 Party Time!");
return (
<div>
<h1>Welcome to the Party!</h1>
<PartyButton color="blue" onClick={handleClick}>
Let's Dance!
</PartyButton>
<PartyButton color="red" onClick={handleClick}>
Fireworks!
</PartyButton>
<PartyButton color="green" onClick={handleClick}>
Go Green!
</PartyButton>
</div>
);
};
export default App;
Why This Is Awesome: You’ve now got a single PartyButton that can throw a dance, fireworks, or eco-friendly bash with just one reusable code! No need to rewrite the button logic every time. It’s a component that loves to party, and you can invite it to any part of your app!
2. The “Card” Component: Because Everyone Loves Cards 🃏
Instead of writing the same card structure over and over for different content, let’s create a reusable Card component that displays any content inside!
import React from "react";
const Card = ({ title, image, description }) => {
return (
<div style={{ border: "1px solid #ddd", padding: "20px", borderRadius: "10px", width: "250px" }}>
<img src={image} alt="Card" style={{ width: "100%", borderRadius: "8px" }} />
<h3>{title}</h3>
<p>{description}</p>
</div>
);
};
const App = () => {
return (
<div>
<h1>Our Awesome Cards</h1>
<Card
title="Cute Cat"
image="https://placekitten.com/300/200"
description="A fluffy cat that will brighten your day."
/>
<Card
title="Epic Mountain"
image="https://placekitten.com/400/300"
description="A majestic mountain, perfect for hiking (and photos)."
/>
</div>
);
};
export default App;
Why This Is Fun:
The Card is like your magic pocket, pulling out cool stuff! It can display anything from cats to mountains, and you don’t have to rewrite the whole structure each time. One card, endless possibilities!
3. The “Avatar” Component: Let’s Put Faces Everywhere 😁
We all love putting faces everywhere, right? Whether it’s your profile pic, a colleague, or your pet hamster, let’s make an Avatar component that accepts an image and a name.
import React from "react";
const Avatar = ({ image, name }) => {
return (
<div style={{ display: "flex", alignItems: "center" }}>
<img
src={image}
alt={name}
style={{
borderRadius: "50%",
width: "50px",
height: "50px",
marginRight: "10px",
}}
/>
<span>{name}</span>
</div>
);
};
const App = () => {
return (
<div>
<h1>Team Avatars</h1>
<Avatar image="https://placekitten.com/200/200" name="Fluffy" />
<Avatar image="https://placekitten.com/300/200" name="Whiskers" />
</div>
);
};
export default App;
Why This Is Fun: Now you can slap an avatar on anything and anyone! It’s like having an unlimited supply of cool headshots for everyone in your app!
4. The “Toggle Switch”: A Light That Always Goes On and Off 💡
What if you need a simple toggle switch for multiple parts of your app, like enabling dark mode, switching settings, or just flicking a light switch for fun? Here’s your reusable ToggleSwitch component!
import React, { useState } from "react";
const ToggleSwitch = ({ label, onChange }) => {
return (
<div>
<span>{label}</span>
<input
type="checkbox"
onChange={onChange}
style={{ marginLeft: "10px", transform: "scale(1.5)" }}
/>
</div>
);
};
const App = () => {
const [isToggled, setIsToggled] = useState(false);
const handleToggle = () => setIsToggled(!isToggled);
return (
<div>
<h1>The Power of the Switch</h1>
<ToggleSwitch label="Enable Dark Mode" onChange={handleToggle} />
<p>{isToggled ? "Dark mode is ON!" : "Light mode is ON!"}</p>
</div>
);
};
export default App;
Why This Is Fun: You can switch it up anywhere you want! Just flick a switch and voila, things change. Plus, it’s reusable, so you can toggle settings, features, or even pretend to control the lights at home!
5. The “Alert” Component: For When You Need to Get Someone’s Attention 🚨
Need a component to alert users about important info, warnings, or something more dramatic like “You forgot to save!”? Here’s your reusable Alert component!
import React from "react";
const Alert = ({ type, message }) => {
const styles = {
success: { backgroundColor: "green", color: "white" },
error: { backgroundColor: "red", color: "white" },
warning: { backgroundColor: "orange", color: "black" },
};
return (
<div style={{ padding: "20px", borderRadius: "5px", ...styles[type] }}>
<strong>{type.toUpperCase()}:</strong> {message}
</div>
);
};
const App = () => {
return (
<div>
<h1>Alert Me, Please!</h1>
<Alert type="success" message="Your changes have been saved!" />
<Alert type="error" message="Oops! Something went wrong!" />
<Alert type="warning" message="Your session is about to expire!" />
</div>
);
};
export default App;
Why This Is Fun: It’s like having a personal PA who pops up and reminds you what’s important, and it never gets tired! Plus, it’s reusable across your entire app — so you can alert users whenever, wherever!
6. The “Toast” Component: Because Everyone Loves a Snack of Information 🍞
Ever needed a little pop-up message to notify users, but you don’t want to block their entire screen? A Toast component is your best friend! It shows brief notifications that disappear after a few seconds — perfect for letting users know things like “Item added to cart” or “Profile updated.”
import React, { useState, useEffect } from "react";
const Toast = ({ message, type }) => {
const styles = {
success: { backgroundColor: "green", color: "white" },
error: { backgroundColor: "red", color: "white" },
info: { backgroundColor: "blue", color: "white" },
};
return (
<div
style={{
position: "absolute",
top: "20px",
right: "20px",
padding: "10px 20px",
borderRadius: "5px",
...styles[type],
}}
>
{message}
</div>
);
};
const App = () => {
const [showToast, setShowToast] = useState(false);
const [toastMessage, setToastMessage] = useState("");
const [toastType, setToastType] = useState("info");
useEffect(() => {
if (showToast) {
const timer = setTimeout(() => setShowToast(false), 3000); // Hide toast after 3 seconds
return () => clearTimeout(timer);
}
}, [showToast]);
const handleShowToast = (message, type) => {
setToastMessage(message);
setToastType(type);
setShowToast(true);
};
return (
<div>
<h1>React Toast Example</h1>
<button onClick={() => handleShowToast("This is a success message!", "success")}>
Show Success Toast
</button>
<button onClick={() => handleShowToast("Oops! Something went wrong.", "error")}>
Show Error Toast
</button>
<button onClick={() => handleShowToast("This is just some information.", "info")}>
Show Info Toast
</button>
{showToast && <Toast message={toastMessage} type={toastType} />}
</div>
);
};
export default App;
Why Reusable Components are Awesome:
Reusable components make your React app cleaner, more maintainable, and much easier to scale. They let you build complex UI elements that can be reused in multiple places, keeping your code DRY (Don’t Repeat Yourself) and preventing the dreaded copy-paste syndrome!
Whether it’s a button, a card, an avatar, a toggle switch, or a toast message, creating components that are reusable ensures that you’re not rewriting the same logic and markup repeatedly. Plus, it’s just fun! 😎
메타데이터
- post_id
- a1ae7db290b7
- slug
- building-reusable-react-components-for-scalable-frontend-development-a1ae7db290b7
- url
- https://medium.com/@pavitra.kini/building-reusable-react-components-for-scalable-frontend-development-a1ae7db290b7
- canonical_url
- https://medium.com/@pavitra.kini/building-reusable-react-components-for-scalable-frontend-development-a1ae7db290b7
- author_url
- https://medium.com/@pavitra.kini
- status
- ok
- fetched_at
- 2026-08-09 17:14:00