π§ React.memo β Boost Performance by Preventing Unnecessary Renders
React.memo is a higher-order component that optimizes performance by skipping re-renders of a component if its props havenβt changed.
π― Why use React.memo?
β’ Improves rendering speed
β’ Saves CPU cycles
β’ Useful for components that render the same UI most of the time
π§ Example:
import React from "react";
const Child = React.memo(function Child({ name }) {
console.log("Child rendered");
return <p>Hello, {name}</p>;
});
export default Child;
π‘ Usage:
<Child name="Aman" />
If the name prop doesnβt change, Child wonβt re-render.
π Key points:
β’ Works only for functional components
β’ Performs a shallow comparison of props
β’ Combine with useCallback or useMemo for best results
React.memo is a simple yet powerful tool for optimizing React apps β but use it wisely, only where itβs needed.
Top comments (0)