Skip to content

How to Fix React.memo Pure Component

DodaTech Updated 2026-06-26 1 min read

The Problem

In this quick-fix, you'll learn how to correctly use React.memo without hiding real updates. This matters because memo that incorrectly prevents re-renders shows stale data. In real-world applications, this pattern appears in display components chart components large lists.

The Wrong Way

const UserCard = React.memo(({ user, onFollow }) => <div><h3>{user.name}</h3></div>,
  (prev, next) => prev.user.id === next.user.id);
function App() {
  const [users, setUsers] = useState([{id:1,name:"John"}]);
  const handleFollow = id => setUsers(p=>p.map(u=>u.id===id?{...u,name:"Updated"}:u));
  return users.map(u => <UserCard key={u.id} user={u} />);
}
When user.name changes UserCard does NOT re-render

The Right Way

const UserCard = React.memo(({ user, onFollow }) => <div><h3>{user.name}</h3></div>);
UserCard re-renders when user.name changes correct behavior

Prevention

  • Use default shallow comparison unless specific reason
  • Immutable updates ensure shallow comparison detects changes
  • Test both updates and skip cases

FAQ

### When does memo actually help??

When component renders often with same props. Comparison cost must be less than rendering cost.

Why does my memo component still re-render??

Shallow comparison fails on inline objects/functions. Use useCallback and useMemo.

Does memo work with forwardRef??

Yes compose: const Fancy = React.memo(React.forwardRef(...));

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