How to use React.memo() ?
How to use React.memo() ?
React.memo() is a higher-order component (HOC) provided by React for optimizing performance in functional components. It is particularly useful when you want to control the re-rendering behavior of components to avoid unnecessary updates, which can lead to improved performance, especially in larger or complex applications.
Basic Usage: React.memo() is used to wrap a functional component. The component will only re-render if its props change. This is done by performing a shallow comparison of the previous and new props.
const MyComponent = React.memo(function MyComponent(props) {
// Component implementation
return <div>{props.children}</div>;
});
Custom Comparison Function:
Optionally, you can provide a second argument to React.memo() — a custom comparison function that tells React how to compare the old props with the new ones. This function should return true if the props are equal (i.e., the component should not re-render), and false if they are not equal (i.e., the component should re-render).
const arePropsEqual = (prevProps, nextProps) => {
Đăng nhập để tham gia thảo luận cùng mọi người.
Chưa có bình luận nào
Hãy là người đầu tiên chia sẻ suy nghĩ của bạn.