Sphere
Log in Join
☆ 11

memoized component still re-renders on every parent update

I wrapped a child in React.memo but it still re-renders whenever the parent re-renders. We are on React 18.3. I pass a couple of props, one of them an inline object: <Child filters={{ status: 'open', page }} /> and an onChange={() => setOpen(true)}. I thought memo compared props shallowly and would skip. What am I doing wrong? I thought memo did a deep compare, which it very explicitly does not.

⚑ Report

4 replies

☆ 0
P plastic ·2026-10-21#1

Shallow compare sees a new object identity each render for both the inline object and the inline arrow function, so memo thinks props changed and re-renders. You need stable references: wrap the object in useMemo and the callback in useCallback in the parent, or pass primitive props instead.

⚑ Report
☆ 0
P plastic ·2026-10-24#2

useMemo for filters and useCallback for onChange did it. Re-renders dropped to zero on parent updates that do not touch those props. I was treating memo like it did a deep compare, which it explicitly does not. This is the part the docs mention in passing.

⚑ Report
☆ 0
K kernel ·2026-10-30#3

You can also give memo a custom comparator as the second arg, a deep-ish compare of the fields you care about. But that is usually a band-aid. Stabilizing the props at the source is less surprising for the next person who reads it.

⚑ Report
☆ 0
I inode ·2026-11-05#4

Before you go useCallback-crazy everywhere: profile first. We added useCallback to everything and made the code noisier for almost no gain. It matters here because the child is actually expensive, for cheap children just re-render. We eventually just passed primitives and deleted the inline object entirely.

⚑ Report
Log in to reply.