Home » React.memo(): Custom Comparison Logic

React.memo(): Custom Comparison Logic

react memo

When using React.memo() to optimize functional components in React, you may want to implement custom comparison logic if the component receives complex objects or arrays as props.

By default, React.memo() performs a shallow comparison of props, which means it only compares the props at the top level and does not perform a deep comparison of nested objects or arrays.

To implement custom comparison logic for objects passed as props, you can create a custom comparison function and pass it as the second argument to React.memo().

Next time you use React.memo(), remember to pass a custom comparison function if your component receives complex objects or arrays as props to ensure correct re-renders and improve performance.

import React from 'react';

function MyComponent({ user }) {
  return (
    <div>
      <h2>{user.name}</h2>
      <p>{user.email}</p>
    </div>
  );
}

function areEqual(prevProps, nextProps) {
  // Compare the user objects by their id property
  return prevProps.user.id === nextProps.user.id;
}

export default React.memo(MyComponent, areEqual);

In this example, MyComponent receives a user object as a prop. The areEqual function compares the user objects by their id property. If the id property is the same in the previous and next props, the function returns true, indicating that the props are equal and the component should not re-render. If the id property is different, the function returns false, indicating that the props are not equal and the component should re-render.

Comparison function That compares nested objects:

import React from 'react';

function MyComponent({ data }) {
  return (
    <div>
      <h2>{data.title}</h2>
      <p>{data.description}</p>
      <ul>
        {data.items.map(item => (
          <li key={item.id}>{item.name}</li>
        ))}
      </ul>
    </div>
  );
}

function areEqual(prevProps, nextProps) {
  // Compare the nested objects by their properties
  return prevProps.data.title === nextProps.data.title &&
         prevProps.data.description === nextProps.data.description &&
         prevProps.data.items.length === nextProps.data.items.length &&
         prevProps.data.items.every((item, index) => item.id === nextProps.data.items[index].id && item.name === nextProps.data.items[index].name);
}

export default React.memo(MyComponent, areEqual);

In this example, MyComponent receives a data object as a prop, which contains a title, a description, and an array of items. The areEqual function compares the data objects by comparing their properties and also compares the items arrays by their id and name properties.

We’re using the every method to compare the items arrays. The every method returns true if every element in an array satisfies a certain condition. In this case, we’re using it to check if every element in the previous items the array has the same id and name properties as the corresponding element in the next items array.

Note that you can use any comparison logic that makes sense for your component. For example, you could compare arrays by their length, or objects by comparing some of their properties.

I hope this helps! Let me know if you have any further questions.


If you’re interested in learning more about how to use hooks like useEffect in your React components, check out our post on Mastering useEffect in React: Tips and Best Practices.” It covers everything from the basics of useEffect to some advanced tips and best practices for using it effectively.
Happy coding!

5 thoughts on “React.memo(): Custom Comparison Logic

Leave a Reply

Your email address will not be published. Required fields are marked *