Preventing Unnecessary Re-renders with React.memo
React.memo is a higherāorder component that shallowly compares props and skips reārendering when they havenāt changed. In mediumāsized apps, frequent reārenders of presentational components (like buttons, cards, or tooltip widgets) can waste CPU cycles, especially when they are deep in the tree or render large lists. By wrapping such components in React.memo, you let React bail out early when the parent updates unrelated state.
Use it judiciously: memoization helps when props are primitive values or stable object references (e.g., using useCallback for handlers). Avoid memoizing components that receive frequently changing objects or arrays unless you stabilize them first, as the shallow compare will deem them different and defeat the purpose. Pair React.memo with useCallback for event handlers and useMemo for expensive derived values to keep referential equality intact. This pattern keeps UI snappy without sacrificing readability, and itās safe to apply broadly to leaf components that are pure in nature.
š»Source Code
// Button.jsx ā a presentational button that rarely needs to reārender
import React from "react";
const Button = React.memo(({ label, onClick, disabled = false }) => {
console.log(`Rendering Button: ${label}`); // for demo; remove in prod
return (
<button onClick={onClick} disabled={disabled} className="btn">
{label}
</button>
);
});
export default Button;
// App.jsx ā demonstrates usage
import React, { useState, useCallback } from "react";
import Button from "./Button";
export default function App() {
const [count, setCount] = useState(0);
const [name, setName] = useState("");
// Stable reference thanks to useCallback
const handleIncrement = useCallback(() => setCount(c => c + 1), []);
return (
<div className="app">
<h1>Counter: {count}</h1>
<Button label="Increment" onClick={handleIncrement} />
{/* This button receives a changing prop (name) ā will reārender on each keystroke */}
<Button label={`Hello, ${name}`} onClick={() => alert(name)} />
<input
value={name}
onChange={e => setName(e.target.value)}
placeholder="Enter name"
/>
</div>
);
}šRelated Snippets
Similar code snippets you might find interesting
Using Intersection Observer for Lazy Loading and Scroll-Based Animations
Leveraging Astro Islands for Interactive Content-Heavy Sites
Maintaining a Simple Global State Store in React Without Extra Libraries
š¬Comments (0)
šPlease login to post comments
No comments yet. Be the first to share your thoughts!
ā”Actions
Share this snippet:
š¤About the Author
admin
Active contributor
