Skip to content

React.memo

一句话解释

React.memo 是 higher-顺序组件那个 memoizes 组件, re-渲染它 仅当 它的 Props 更改.

用大白话说

React.memo wraps a function component and prevents re-renders when the parent re-renders, if the component's props haven't changed. It does a shallow comparison of props by default.

const MemoizedComponent = React.memo(MyComponent) — now, if the parent re-renders but passes the same props, MemoizedComponent skips rendering and reuses the last result.

You can pass a custom comparison function as the second argument: React.memo(MyComponent, areEqual). This is useful when props contain objects or arrays that are structurally equal but referentially different.

Use React.memo for components that render expensively or re-render often with unchanged props. But remember: if the parent always passes new object/array/function props, memo won't help unless you also use useMemo/useCallback to stabilize those props.

示例

jsx
const ExpensiveList = React.memo(function ExpensiveList({ items }) {
  return items.map(item => <div key={item.id}>{item.name}</div>);
});

// Only re-renders when `items` reference changes

React.memo prevents ExpensiveList 从 re-渲染当 它的 parent re-渲染, as 长 as 项 prop hasn't 更改.

相关术语

相关资源

相关课程

解读错误信息

用大白话解释常见错误——什么意思、为什么发生、如何修复。

查看错误

← 返回术语表