跳到正文
cccskills
免费在 GitHub 公开

optimizing-react-performance

React performance optimization. Identify unnecessary renders, apply memo/useCallback/useMemo correctly.

中文简介正在准备中。当前显示原文说明。

查看安装方法

包含的文件(1)

  • SKILL.md3.5 KB

SKILL.md(原文)

安装前可以查看交给 AI 代理的指令内容。

Profile First — Never Optimize Blindly

// 1. Enable React DevTools Profiler
// 2. Record the slow interaction
// 3. Find components with long render times

// Rule: Only optimize what the profiler shows as slow

The Three Memoization Tools

React.memo — Prevent Child Re-renders

// Without memo: re-renders whenever parent re-renders (even same props)
const ExpensiveList = React.memo(({ items, onItemClick }) => {
  return items.map(item => (
    <Item key={item.id} item={item} onClick={onItemClick} />
  ));
});

// Only re-renders when items or onItemClick reference changes

useCallback — Stable Function References

// Without useCallback: new function reference every render → breaks React.memo
const Parent = () => {
  const [count, setCount] = useState(0);

  // BAD: new reference every render
  const handleClick = (id) => console.log(id);

  // GOOD: stable reference (only changes if count changes)
  const handleClick = useCallback((id) => {
    console.log(id, count);
  }, [count]);

  return <ExpensiveList onItemClick={handleClick} />;
};

useMemo — Expensive Computations

// Only use useMemo for genuinely expensive calculations
const sortedAndFilteredItems = useMemo(() => {
  return items
    .filter(item => item.active)
    .sort((a, b) => b.priority - a.priority);
}, [items]); // only recalculate when items changes

Anti-Patterns

// Anti-pattern 1: Premature optimization
const name = useMemo(() => `${first} ${last}`, [first, last]);
// String concatenation is instant — useMemo adds overhead here

// Anti-pattern 2: Object in deps breaks memoization
const options = { limit: 10 };
const result = useMemo(() => fetchData(options), [options]);
// options is a new object every render! Memoization is pointless.

// Fix: primitives in deps
const result = useMemo(() => fetchData({ limit }), [limit]);

Virtualization for Long Lists

// Don't render 10,000 items in the DOM
import { FixedSizeList } from 'react-window';

const VirtualList = ({ items }) => (
  <FixedSizeList height={600} width="100%" itemCount={items.length} itemSize={50}>
    {({ index, style }) => (
      <div style={style}>{items[index].name}</div>
    )}
  </FixedSizeList>
);

State Colocation

// Don't put state in parent if only child needs it
// BAD: every sibling re-renders when modalOpen changes
const Parent = () => {
  const [modalOpen, setModalOpen] = useState(false);
  return (
    <>
      <HeavyComponent />  {/* re-renders on every modal toggle */}
      <Modal open={modalOpen} onClose={() => setModalOpen(false)} />
    </>
  );
};

// GOOD: colocate state
const ModalWithState = () => {
  const [open, setOpen] = useState(false);
  return <Modal open={open} onClose={() => setOpen(false)} />;
};
const Parent = () => (
  <>
    <HeavyComponent />  {/* never re-renders for modal */}
    <ModalWithState />
  </>
);

Quick Wins Checklist

  • Add keys to all lists (use stable IDs, not array index)
  • Lazy-load heavy components: const Chart = lazy(() => import('./Chart'))
  • Code-split routes (Next.js does this automatically)
  • Move static values outside component body
  • Use virtualization for lists > 100 items

评价

还没有评价。欢迎分享你的使用感受。

评论

无需登录即可发表。欢迎分享使用感受、提问,或说明在哪些环境下无法运行。

还没有评论。来写第一条吧。

提交后会立即公开。请勿填写邮箱、电话等个人信息。URL 最多 2 个。

0 / 1000 字

此表单受 reCAPTCHA 保护,适用 Google 的隐私权政策和服务条款。

同一仓库的技能

anoa

免费

Drive a real browser from the command line — open pages, snapshot the interactive elements as refs, click and fill by ref, read text, run JS, screenshot. Use when a task needs a live browser: checking a page renders, walking a login or checkout flow, scraping something that only exists after JavaScript runs, or reproducing a UI bug.

中文简介正在准备中。当前显示原文说明。

porcupine-md/jonggrang162026年9月15日 更新

Run linters/typecheckers to calculate health score.

中文简介正在准备中。当前显示原文说明。

porcupine-md/jonggrang162026年9月15日 更新

Comprehensive UI/UX compliance check (A11y, performance, responsive).

中文简介正在准备中。当前显示原文说明。

porcupine-md/jonggrang162026年9月15日 更新

Infrastructure-first security audit (OWASP, secrets, dependencies).

中文简介正在准备中。当前显示原文说明。

porcupine-md/jonggrang162026年9月15日 更新

auth

免费

Setup authentication flow (register, login, session/JWT, middleware)

中文简介正在准备中。当前显示原文说明。

porcupine-md/jonggrang162026年9月15日 更新

Deep knowledge about deploying applications to AWS (EC2, ECS, S3, CloudFront, Cloud Run).

中文简介正在准备中。当前显示原文说明。

porcupine-md/jonggrang162026年9月15日 更新

查看 porcupine-md 的全部技能

报告此技能的问题