Component Reusability
Component reusability in React: build once, reuse many times with props, children, and composition while avoiding over-engineering.
Component Reusability
Component reusability is one of the biggest advantages of React.
Build a component once, then use it multiple times with different props, content, or behavior.
Instead of writing the same UI repeatedly, create reusable components and customize through props.
Learning Objectives
By the end of this lesson, you will understand:
- What component reusability means
- Why reusable components matter
- How to identify reusable UI
- How to create reusable components
- How props customize behavior and data
- How children enables composition
- Reusable buttons, cards, inputs, and layouts
- How to avoid over-engineering
- How to design component APIs
- Common mistakes and practical fixes
1. What Is Component Reusability?
Suppose you need three buttons:
SaveCancelDelete
A beginner might create three separate components with duplicate structure.
function SaveButton() { return <button>Save</button>; }function CancelButton() { return <button>Cancel</button>; }function DeleteButton() { return <button>Delete</button>; }
Reusable approach:
function Button({ children }: { children: React.ReactNode }) {return <button>{children}</button>;}
<Button>Save</Button><Button>Cancel</Button><Button>Delete</Button>
One component. Many uses.
2. Why Reusability Matters
- Less duplication: common UI logic is written once.
- Easier maintenance: one update benefits all usages.
- Consistency: shared structure and behavior across pages.
- Faster development: assemble pages from existing blocks.
- Better organization: large apps become smaller composable units.
3. Think in UI Building Blocks
Application│├── Navbar├── Sidebar├── PageHeader├── SearchBox├── ProductCard├── Button└── Footer
Each block can potentially be reused across multiple pages and workflows.
4. The Basic Pattern
function Component(props: { value: string }) {return <div>{props.value}</div>;}<Component value="Hello" />
The component holds common structure. Props hold differences.
5. Example: Reusable Product Card
function ProductCard({ name, price, image }: { name: string; price: number; image: string }) {return (<article><img src={image} alt={name} /><h2>{name}</h2><p>INR {price}</p></article>);}
<ProductCard name="Banarasi Silk Saree" price={4999} image="/saree1.jpg" /><ProductCard name="Kanchipuram Silk Saree" price={6999} image="/saree2.jpg" /><ProductCard name="Designer Saree" price={3999} image="/saree3.jpg" />
6. Props Make Reusability Possible
Without props, a component is tied to hardcoded content. With props, it becomes reusable.
7. Component Reuse Through children
function Card({ children }: { children: React.ReactNode }) {return <div className="card">{children}</div>;}
<Card><h2>Profile</h2><p>Welcome back!</p></Card><Card><h2>Orders</h2><p>You have 5 orders.</p></Card>
8. Reusable Button
function Button({children,variant = "primary",onClick,}: {children: React.ReactNode;variant?: "primary" | "secondary" | "danger";onClick?: () => void;}) {return (<button className={`button ${variant}`} onClick={onClick}>{children}</button>);}
9. Reusable Input
function Input({type = "text",placeholder,value,onChange,}: {type?: string;placeholder: string;value?: string;onChange?: (e: React.ChangeEvent<HTMLInputElement>) => void;}) {return <input type={type} placeholder={placeholder} value={value} onChange={onChange} />;}
10. Reusable Avatar
function Avatar({ src, name, size = 50 }: { src: string; name: string; size?: number }) {return <img src={src} alt={name} width={size} height={size} />;}
11. Reusable Alert
function Alert({ type, children }: { type: "success" | "error" | "warning"; children: React.ReactNode }) {return <div className={`alert ${type}`}>{children}</div>;}
12. Reusable Navbar
function Navbar({ logo, children }: { logo: string; children: React.ReactNode }) {return (<nav><img src={logo} alt="Logo" /><div>{children}</div></nav>);}
13. Reusable Layout Components
function PageLayout({ children }: { children: React.ReactNode }) {return (<div className="page-layout"><Navbar logo="/logo.png"><a href="/">Home</a></Navbar><main>{children}</main><footer>Footer</footer></div>);}
14. Component Composition
ProductPage│├── Navbar├── ProductGallery├── ProductInfo│ ├── Price│ ├── Rating│ └── Button└── Footer
Composition helps you build large UI from focused smaller components.
15. Reusability Through Composition
<Card><ProductInfo /></Card><Card><UserProfile /></Card><Card><OrderSummary /></Card>
16. Clear Responsibilities
Good reusable components have focused responsibilities: Button, Input, Avatar, Card, Modal.
Avoid one giant everything component that owns unrelated concerns.
17. Do Not Extract Everything
Not every repeated JSX fragment needs extraction. Extract when clarity, reuse, or composition improves.
18. Avoid Premature Abstraction
Start simple. Extract after patterns become clear and repeated in meaningful ways.
19. The Rule of Repetition
If the same structure appears many times, ask: Can this become a reusable component?
20. Reuse Data, Not Just Markup
const products = [{ id: 1, name: "Banarasi Silk", price: 4999 },{ id: 2, name: "Kanchipuram Silk", price: 6999 },{ id: 3, name: "Cotton Saree", price: 1999 },];function App() {return (<div>{products.map((product) => (<ProductCard key={product.id} name={product.name} price={product.price} image="/placeholder.jpg" />))}</div>);}
21. A Better ProductCard API
<ProductCard product={product} />function ProductCard({ product }: { product: { name: string; price: number; image: string } }) {return (<article><img src={product.image} alt={product.name} /><h2>{product.name}</h2><p>INR {product.price}</p></article>);}
22. Designing a Component API
A good component API is clear, predictable, easy to use, hard to misuse, and purposefully flexible.
<Button variant="primary" size="large" disabled={false} onClick={handleClick}>Save</Button>
23. Avoid Too Many Special Cases
Prefer meaningful variants and sizes instead of many booleans like isBlue, isBig, isSpecial.
24. Reusable Components and Events
function SearchBox({value,onChange,onSubmit,}: {value: string;onChange: (e: React.ChangeEvent<HTMLInputElement>) => void;onSubmit: (e: React.FormEvent<HTMLFormElement>) => void;}) {return (<form onSubmit={onSubmit}><input value={value} onChange={onChange} /><button type="submit">Search</button></form>);}
25. Reusable Modal
function Modal({ title, children, onClose }: { title: string; children: React.ReactNode; onClose: () => void }) {return (<div className="modal"><div className="modal-content"><h2>{title}</h2><div>{children}</div><button onClick={onClose}>Close</button></div></div>);}
26. Reusable Components and Conditional UI
function StatusBadge({ status }: { status: "success" | "error" | "pending" }) {if (status === "success") return <span>Success</span>;if (status === "error") return <span>Error</span>;return <span>Pending</span>;}
27. Reusable Loading Component
function Loading({ message = "Loading..." }: { message?: string }) {return (<div><span>Loading...</span><p>{message}</p></div>);}
28. Reusable Empty State
function EmptyState({title,message,action,}: {title: string;message: string;action?: React.ReactNode;}) {return (<section><h2>{title}</h2><p>{message}</p>{action}</section>);}
29. Reusable Components in a Real Project
src/├── components/│ ├── Button.jsx│ ├── Input.jsx│ ├── Card.jsx│ ├── Modal.jsx│ ├── Navbar.jsx│ └── ProductCard.jsx├── pages/│ ├── Home.jsx│ ├── Products.jsx│ └── Checkout.jsx└── App.jsx
30. Component Reuse Across Pages
Home: Navbar + ProductCard + FooterProducts: Navbar + ProductCard + FooterCheckout: Navbar + Button + Footer
31. Reusability and Consistency
When behavior or styles improve in one shared component, all consumers benefit.
32. Reusable Does Not Mean Universal
Do not force every domain into one mega generic component if focused components are clearer.
33. Reusable vs Specialized Components
Reusable: Button, Input, Modal, Card. Specialized: CheckoutSummary, OrderTracking.
34. Hide Implementation Details
Consumers should use component intent, not internal CSS details.
35. Before Reusability
function Home() { return <button className="primary-button">Buy</button>; }function ProductPage() { return <button className="primary-button">Add to Cart</button>; }function Checkout() { return <button className="primary-button">Pay</button>; }
36. After Reusability
function Button({ children, onClick }: { children: React.ReactNode; onClick?: () => void }) {return (<button className="primary-button" onClick={onClick}>{children}</button>);}
37. Reusability + Props + Composition
Component├── props (data)├── children (content)└── callbacks (behavior)↓Reusable UI
38. A Complete Reusable Card
function Card({title,description,children,footer,}: {title: string;description: string;children: React.ReactNode;footer?: React.ReactNode;}) {return (<article className="card"><header><h2>{title}</h2><p>{description}</p></header><div className="card-body">{children}</div>{footer && <footer>{footer}</footer>}</article>);}
39-43. Common Mistakes
- Hardcoding data in components meant to be reused.
- Creating overly generic universal components with too many flags.
- Duplicating near-identical components instead of parameterizing.
- Over-abstraction that hides real domain differences.
- Ignoring composition and forcing everything through props.
44. Reusability Is About Abstraction
Repeated UI↓Identify common structure↓Extract component↓Expose meaningful props↓Reuse component
45. Practical Project Exercise
Create:
components/Button.jsxcomponents/Card.jsxcomponents/ProductCard.jsxcomponents/Navbar.jsx
Then build a product listing page by composing those components.
46. The Reusability Test
- What part is common?
- What part changes?
- Can the changing part become props?
- Would children improve flexibility?
- Does extraction make code clearer?
47. Interview Questions
Q1. What is component reusability?
Component reusability is designing components that can be used in multiple contexts with different data, content, or behavior.
Q2. How do props help with reusability?
Props allow one component implementation to behave differently per use.
Q3. What is composition?
Composition combines smaller components to build larger features.
Q4. Should every repeated JSX fragment become a component?
No. Extract when it improves clarity, maintainability, or meaningful reuse.
Q5. What makes a good reusable component?
Clear responsibility, simple API, predictable behavior, and sensible composition points.
48. Key Mental Model
Reusable Component├── Data -> props├── Content -> children└── Behavior -> callbacks↓Flexible UI
Final Takeaway
Component reusability is about building UI once and using it many times without duplicating implementation.
The most important tools are props, children, and composition.
Strong React engineers ask: what is stable in this component, what should be configurable, and what should remain internal?