Skip to content

How to Fix React.memo Compare Function

DodaTech Updated 2026-06-26 1 min read

The Problem

In this quick-fix, you'll learn how to write correct custom comparison for React.memo. This matters because incorrect comparison blocks or fails to prevent re-renders. In real-world applications, this pattern appears in optimizing components with deeply nested objects.

The Wrong Way

const DataGrid = React.memo(({ data }) => <div>...</div>,
  (prev, next) => { return prev.data !== next.data; });
DataGrid doesnt re-render when data changes comparison inverted

The Right Way

const DataGrid = React.memo(({ data }) => <div>...</div>,
  (prev, next) => { return prev.data === next.data; });
DataGrid correctly skips render when data reference same

Prevention

  • Compare function returns true to skip false to re-render
  • Default shallow comparison correct for immutable data
  • Deep comparison has perf cost

FAQ

### Does the compare function receive all props??

Yes receives prevProps and nextProps. Compare only what affects rendering.

Can I use deep equality in compare??

Yes lodash.isequal but has performance cost every render. Use only when necessary.

How to debug memo comparison??

Log in compare function to see what changes between renders.

This guide is brought to you by DodaTech -- built by the developers of Doda Browser, DodaZIP, and Durga Antivirus Pro.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro