Component reusability is one of the core strengths of React, but as projects grow in size, managing reusable components effectively becomes more challenging. In this blog post, we'll start with the basics and gradually dive into advanced strategies, using real-life examples and large-scale implementation tips.
🌱 Getting Started: What is Component Reusability?
At its core, component reusability means creating UI components that can be reused in different parts of your application without rewriting the same logic or layout.
Simple Example:
const Button = ({ label, onClick }) => {
return <button onClick={onClick}>{label}button>;
};
You can use this Button
across your entire app just by changing label
and onClick
.
🧩 Step Up: Making Components More Flexible
As your app grows, your components need to handle different use cases.
Add Variants:
const Button = ({ label, onClick, variant = "primary" }) => {
const styles = {
primary: "bg-blue-500 text-white",
secondary: "bg-gray-200 text-black",
};
return <button onClick={onClick} className={styles[variant]}>{label}button>;
};
Now, you can use it like:
<Button label="Save" variant="primary" />
<Button label="Cancel" variant="secondary" />
Real-World Scenario:
Think of an e-commerce site. The same button might be used for "Add to Cart," "Buy Now," or "Remove from Wishlist." Instead of creating three separate buttons, one flexible Button
component can handle all these cases.
📦 Organizing a Component Library
Folder Structure for Scalability:
src/
├── components/
│ ├── Button/
│ │ ├── Button.jsx
│ │ └── Button.module.css
│ ├── Modal/
│ │ └── Modal.jsx
│ └── index.js
This structure helps in:
- Clear separation of concerns
- Easy reusability
- Scalability across teams
Index File Example:
export { default as Button } from './Button/Button';
export { default as Modal } from './Modal/Modal';
Now you can import easily:
import { Button, Modal } from '../components';
🎯 Advanced Technique for Reusability
Higher-Order Components (HOC)
A Higher-Order Component is a function that takes a component and returns a new enhanced component.
Think of it like a wrapper function that adds some extra behavior to your original component — without changing it.
Example:
const withLogger = (Component) => (props) => {
console.log("Rendering", Component.name);
return <Component {...props} />;
};
const LoggedButton = withLogger(Button);
withLogger
is a HOC that logs when the component is rendered.
LoggedButton
now behaves like Button
, but with extra logging.
Use Case:
Say you're building an analytics dashboard. You want to track when components render or user interactions happen — instead of adding console.log
to each component, just use a HOC.
Real Life:
Authentication check (
withAuth
)Error boundaries (
withErrorBoundary
)Feature flags (
withFeatureToggle
)
Use HOCs to wrap logic around reusable components.
const withLogger = (Component) => (props) => {
console.log("Rendering", Component.name);
return <Component {...props} />;
};
const LoggedButton = withLogger(Button);
🏢 Real-Life Use Case: Large-Scale E-commerce Platform
Imagine building an e-commerce platform with:
- Product Cards used on Home, Category, and Wishlist pages
- Rating Stars used in Reviews, Product Cards, and Order History
- Price Formatter used across checkout, cart, and product details
To avoid duplication:
- Create generic
ProductCard
,Rating
, andPrice
components. - Use props and slots to handle differences.
- Manage styling via CSS modules or Tailwind variants.
- Extract logic like currency conversion into custom hooks.
🚀 Tips for Scaling Component Reusability
- ✅ Use TypeScript to ensure prop safety
- ✅ Document components with Storybook
- ✅ Create a design system or UI library (e.g., using Tailwind + Radix)
- ✅ Keep business logic outside of UI components
🧠 Final Thoughts
Component reusability is not just about DRY (Don’t Repeat Yourself) — it’s about building systems that are scalable, maintainable, and easy to collaborate on. Start simple, but always design with flexibility in mind.
The more your app grows, the more important reusability becomes. Treat every component like it might be reused 100 times — because in large projects, it just might be.
Happy coding! 💻