Urgent.News

One page, thousands of outlets. See who else covered it.

Editions

Tech

React.memo Doesn't Do What You Think

What React.memo actually checks React.memo wraps a component so that, before re-rendering it, React does a shallow comparison of its new props against its previous props. If every prop is === to what it was last time, React skips re-rendering that component and reuses the previous output. "Shallow" is the key word — it compares object and function props by reference , not by deep value, which is…

React.memo is a tool that prevents components from re-rendering if their props haven't changed. It performs a shallow comparison of props, checking if they are the same reference, not if their values are identical. This can lead to confusion when developers expect a child component to render once, but it re-renders every time the parent does.

The issue typically stems from props being passed into the child component that change on every render, even if the prop values logically remain the same. For example, passing a function or object literal that is created anew on each render will cause React.memo to fail in its shallow comparison. The fix is to use useCallback and useMemo to create stable references for props that should remain unchanged between renders.

This ensures that React.memo can accurately detect whether the props are genuinely the same, allowing the component to skip unnecessary re-rendering.

Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.

Read the original at dev.to →

More in Tech

More from Monday 17 August →