When building React applications, two concepts help developers create:
β
Faster applications
β
Cleaner code
β
Reusable components
β
Better maintainability
Those concepts are:
React.memo
- Component Composition
In this guide, youβll learn both concepts clearly with real-world examples.
What You Will Learn
β What is React.memo
β Why we need it
β How it improves performance
β What is Composition
β Why Composition is preferred in React
β Real-world examples for both
Part 1: Understanding React.memo
What is React.memo?
Simple Definition
React.memo is a higher-order component that prevents unnecessary re-rendering of functional components.
Why Do We Need React.memo?
In React:
π Parent component re-renders
β‘ Child components also re-render
Even if:
- Props did not change
- UI did not change
This can reduce performance.
Example Without React.memo
β Code
import { useState } from "react";
function Child() {
console.log("Child Rendered");
return <h2>Child Component</h2>;
}
function App() {
const [count, setCount] = useState(0);
return (
<div>
<h1>{count}</h1>
<button onClick={() => setCount(count + 1)}>
Increment
</button>
<Child />
</div>
);
}
export default App;
What Happens Here?
Whenever count changes:
setCount(count + 1)
π Parent re-renders
π Child ALSO re-renders
Even though Child has no relation to count.
Optimized with React.memo
β
Code
import { memo } from "react";
const Child = memo(function Child() {
console.log("Child Rendered");
return <h2>Child Component</h2>;
});
export default Child;
Now What Happens?
When parent re-renders:
π React compares previous props vs new props
If props are same:
β
Child render is skipped
Important Rule
React.memo works best when:
β Props are stable
β Component renders frequently
β Rendering is expensive
β οΈ Example Where React.memo Fails
β Problem
<Child data={{ name: "React" }} />
Why?
Because:
{ name: "React" }
creates a NEW object every render.
π React thinks props changed.
Fix with useMemo
const data = useMemo(() => ({
name: "React"
}), []);
Real-World Example
Imagine:
- Dashboard with charts
- Huge tables
- Complex UI
Without optimization:
β Unnecessary renders
β Slow UI
With React.memo:
β
Better performance
When NOT to Use React.memo
Avoid if:
- Component is very small
- Props change frequently
- Optimization not needed
π Overusing memo can increase complexity.
Interview Tip
β What is React.memo?
π Answer:
React.memo prevents unnecessary re-rendering of functional components by memoizing rendered output based on props comparison.
Part 2: Understanding Composition in React
π What is Composition?
Simple Definition
Composition means building complex UI using smaller reusable components.
Basic Composition Example
πΉ Button Component
function Button({ children }) {
return (
<button>
{children}
</button>
);
}
πΉ Usage
<Button>Save</Button>
<Button>Delete</Button>
Why Composition is Powerful
Composition helps:
β
Reusability
β
Cleaner architecture
β
Scalability
β
Separation of concerns
Composition with Layouts
Card Component
function Card({ children }) {
return (
<div className="card">
{children}
</div>
);
}
Usage
<Card>
<h2>React</h2>
<p>Composition Example</p>
</Card>
Composition Using Props
Modal Component
function Modal({ title, content, footer }) {
return (
<div className="modal">
<h2>{title}</h2>
<div>{content}</div>
<footer>{footer}</footer>
</div>
);
}
Usage
<Modal
title="Delete User"
content={<p>Are you sure?</p>}
footer={<button>Confirm</button>}
/>
Composition vs Inheritance
React documentation recommends:
β
Composition
β Inheritance
Inheritance Problem
Inheritance creates:
- Tight coupling
- Complexity
Composition Advantage
Composition provides:
- Flexibility
- Better maintainability
Advanced Composition Pattern
Compound Components
Example:
<Tabs>
<Tabs.List />
<Tabs.Panel />
</Tabs>
Used in:
- UI libraries
- Design systems
Combining React.memo + Composition
This is common in real applications.
Example
const Card = memo(function Card({ children }) {
return (
<div className="card">
{children}
</div>
);
});
π Reusable + optimized
Common Mistakes
β Overusing React.memo
Not every component needs memoization.
β Creating Huge Components
Break UI into reusable pieces.
β Deep Prop Drilling
Use:
- Context API
- Composition patterns
Best Practices
β
Use React.memo Carefully
Optimize only where needed.
β
Prefer Composition
Build UI using reusable blocks.
β
Keep Components Small
Small components:
- Easier to test
- Easier to reuse
Final Summary
React.memo
β Prevents unnecessary re-renders
β Improves performance
β Best for stable props
Composition
β Builds reusable UI
β Cleaner architecture
β Recommended React pattern
π‘ Found this helpful? Subscribe for simple React guides, real-world examples, and interview preparation tips. Happy Coding!