Skip to content
Tutorials Center
My learning

Topics

React Reference ยท Top-Level React APIs

React memo() function

Reference for React memo: wrap a component so it skips re-rendering when its props are unchanged, with the custom comparison function and the pitfalls.

memo wraps a component and returns a new component that remembers its last rendered result. When the parent re-renders, React compares the new props with the previous ones. If every prop is equal, React reuses the previous result and skips calling your component.

It is a performance tool, not a correctness tool: your app must behave the same with or without it. Use it for components that are expensive to render and often receive the same props.

JSX
import { memo } from 'react';

const Row = memo(function Row({ label }) {
  return <li>{label}</li>;
});

Try it

Skipped when props are equal
New objects defeat memo

After one click on the button in the second example, the console shows Chart rendered: inline object again but not the memoized one: the inline { color: 'teal' } is a different object on every render, so the shallow comparison fails.

Syntax

TypeScript
function memo<P>(
  Component: ComponentType<P>,
  arePropsEqual?: (prevProps: P, nextProps: P) => boolean
): MemoExoticComponent<ComponentType<P>>;
Name Description
Component The component to wrap. Usually a function component; forwardRef components and other memo-compatible components also work.
arePropsEqual Optional. Receives the old and new props and returns true if rendering can be skipped. Without it, React compares each prop with Object.is.

Returns: a new component you use exactly like the original. It has the same props.

Usage notes

  • Shallow comparison. Each prop is compared with Object.is. Strings, numbers and booleans are fine; objects, arrays and functions are equal only if they are the same reference. Stabilize them with useMemo and useCallback in the parent, or pass primitives.
  • Own state and context still re-render it. memo only guards against re-renders caused by the parent. A useState update inside the component, or a change in a context it reads, renders it regardless.
  • children is a prop too. JSX children are a new element object on every parent render, so a memoized component that receives <b>text</b> as children is never skipped.
  • A custom arePropsEqual is rarely worth it. Deep comparisons can cost more than the render they save, and forgetting a prop (such as a callback) produces stale UI. Prefer fixing the props.
  • Do not memoize everything. Each memo adds a comparison. Measure first with the Profiler. Components that are cheap, or whose props change every time, gain nothing.
  • React Compiler can add this kind of memoization automatically at build time. If you use it, you rarely write memo by hand.
  • Naming: pass a named function (memo(function Row() {})) so DevTools show a useful name. An anonymous arrow shows up as Anonymous.
  • Never call memo inside a component. Create the memoized component once at module level; otherwise a new component type is created on every render and state is lost.