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!