前置知识: React

React性能优化

00:00
1 min Intermediate 2026/6/14

React应用性能优化策略

1. 避免不必要渲染

// React.memo
const MyComponent = React.memo(function MyComponent(props) {
  return <div>{props.value}</div>;
});

// useMemo
const expensiveValue = useMemo(() => computeExpensive(a, b), [a, b]);

// useCallback
const handleClick = useCallback(() => doSomething(id), [id]);

2. 代码分割

const LazyComponent = React.lazy(() => import('./HeavyComponent'));

<Suspense fallback={<Loading />}>
  <LazyComponent />
</Suspense>;

3. 虚拟化长列表

import { FixedSizeList } from 'react-window';

function MyList({ items }) {
  return (
    <FixedSizeList height={600} itemCount={items.length} itemSize={50}>
      {({ index, style }) => <div style={style}>{items[index].name}</div>}
    </FixedSizeList>
  );
}

4. Profiler

<Profiler
  id="Panel"
  onRender={(id, phase, duration) => {
    console.log(`${id} ${phase} took ${duration}ms`);
  }}
>
  <Panel />
</Profiler>

知识检测

学习进度

-- 已学文档
--% 知识覆盖率

学习推荐

专注模式