03 Developer ReactBestPractices

From boshi-xixixi/TraeSkill · View interactive version

03 Developer ReactBestPractices is an AI agent skill from the GitHub repository boshi-xixixi/TraeSkill. 当用户需要编写、审查或重构 React/Next.js 代码时使用。此 Skill 提供来自 Vercel Engineering 的 React 和 Next.js 性能优化指南,包含 45 条规则,涵盖组件优化、bundle 大小、数据获取等方面。 触发词:React 性能、水合报错、useEffect 写错了、Next.js 变慢。

How to use this skill

Skill file

⚠️ 性能提示: 此 Skill 包含 45 个 rules 文件(L1 ~50KB),专注于 React 性能优化。请根据实际场景选择性应用规则,优先处理 CRITICAL 和 HIGH 优先级的规则。


Vercel React Best Practices

Comprehensive performance optimization guide for React and Next.js applications, maintained by Vercel. Contains 45 rules across 8 categories, prioritized by impact to guide automated refactoring and code generation.


动画性能规范(Animation Performance)

在 React 应用中实现高性能动画,遵循 compositor-only 原则。

核心原则

浏览器可以在 compositor thread 上独立于主线程处理两种属性:

所有其他属性(width, height, top, left, background 等)必须在主线程上计算,可能导致卡顿。

只允许的属性

/* ✅ 允许:transform */
transform: translateX(100px);
transform: scale(1.1);
transform: rotate(45deg);
transform: skewX(10deg);

/* ✅ 允许:opacity */
opacity: 0;
opacity: 1;

禁止的属性

/* ❌ 禁止:布局属性 */
width: 100%;
height: 200px;
top: 0;
left: 0;
margin: 10px;
padding: 20px;

/* ❌ 禁止:可能触发布局的属性 */
background-color: red;
border: 1px solid black;
color: blue;

动画实现示例

// ❌ 不合规:使用 height 动画
function ExpandingBox() {
  const [isExpanded, setIsExpanded] = useState(false);

  return (
    <div
      style={{
        height: isExpanded ? '200px' : '50px',
        transition: 'height 0.3s ease'
      }}
    >
      内容
    </div>
  );
}

// ✅ 合规:使用 transform 和 max-height
function ExpandingBox() {
  const [isExpanded, setIsExpanded] = useState(false);

  return (
    <div
      style={{
        maxHeight: isExpanded ? '200px' : '50px',
        overflow: 'hidden',
        transition: 'max-height 0.3s ease'
      }}
    >
      内容
    </div>
  );
}

// ✅ 最佳:纯 transform
function ExpandingBox() {
  const [isExpanded, setIsExpanded] = useState(false);

  return (
    <button onClick={() => setIsExpanded(!isExpanded)}>
      <span style={{
        transform: isExpanded ? 'rotate(180deg)' : 'rotate(0)',
        transition: 'transform 0.3s ease'
      }}>
        ▼
      </span>
      内容
    </button>
  );
}

React 动画库使用规范

Framer Motion / Motion
// ✅ 合规:使用 motion.div
import { motion } from 'framer-motion';

function AnimatedComponent() {
  return (
    <motion.div
      initial={{ opacity: 0, x: -100 }}
      animate={{ opacity: 1, x: 0 }}
      transition={{ duration: 0.5, ease: 'easeOut' }}
    >
      内容
    </motion.div>
  );
}

// ✅ 合规:使用 useTransform 限制属性
import { motion, useTransform } from 'framer-motion';

function ParallaxHeader() {
  const scrollY = useScroll();
  const y = useTransform(scrollY, [0, 500], [0, -200]);

  return (
    <motion.header style={{ y }}>
      内容
    </motion.header>
  );
}
Tailwind CSS 动画
// ✅ 合规:使用 transform
<div className="transform hover:scale-105 transition-transform duration-200">
  内容
</div>

// ❌ 不合规:使用 width/height
<div className="w-0 hover:w-full transition-all duration-300">
  内容
</div>

性能检查清单

必须遵守
必须实现
/* will-change 提示 */
.animated-element {
  will-change: transform, opacity;
}

/* prefers-reduced-motion 支持 */
@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }
}
// 暂停视口外动画
import { useInView } from 'framer-motion';

function LazyAnimation() {
  const ref = useRef(null);
  const isInView = useInView(ref, { once: true, margin: '-100px' });

  return (
    <motion.div
      ref={ref}
      initial={{ opacity: 0 }}
      animate={isInView ? { opacity: 1 } : { opacity: 0 }}
    >
      内容
    </motion.div>
  );
}

常见错误示例

// ❌ 错误 1:宽度动画
function Modal() {
  return (
    <div style={{
      width: isOpen ? '100%' : '0',
      transition: 'width 0.3s'
    }} />
  );
}

// ✅ 正确:使用 transform
function Modal() {
  return (
    <div style={{
      transform: isOpen ? 'scaleX(1)' : 'scaleX(0)',
      transformOrigin: 'left'
    }} />
  );
}

// ❌ 错误 2:颜色动画
function Button() {
  return (
    <button style={{
      backgroundColor: isHovered ? '#ff0000' : '#0000ff',
      transition: 'background-color 0.3s'
    }} />
  );
}

// ✅ 正确:使用 opacity 或绝对定位覆盖
function Button() {
  return (
    <button className="relative">
      <span className="bg-blue-500" />
      <span
        className="absolute inset-0 bg-red-500 opacity-0 hover:opacity-100 transition-opacity"
      />
    </button>
  );
}

// ❌ 错误 3:位置动画
function Draggable() {
  return (
    <div style={{
      top: y,
      left: x,
      transition: 'top 0.2s, left 0.2s'
    }} />
  );
}

// ✅ 正确:使用 transform
function Draggable() {
  return (
    <motion.div
      drag
      style={{ x, y }}
    />
  );
}

When to Apply

Reference these guidelines when:


Rule Categories by Priority

| Priority | Category | Impact | Prefix |

|----------|----------|--------|--------|

| 1 | Eliminating Waterfalls | CRITICAL | async- |

| 2 | Bundle Size Optimization | CRITICAL | bundle- |

| 3 | Server-Side Performance | HIGH | server- |

| 4 | Client-Side Data Fetching | MEDIUM-HIGH | client- |

| 5 | Re-render Optimization | MEDIUM | rerender- |

| 6 | Rendering Performance | MEDIUM | rendering- |

| 7 | JavaScript Performance | LOW-MEDIUM | js- |

| 8 | Advanced Patterns | LOW | advanced- |


Quick Reference

1. Eliminating Waterfalls (CRITICAL)

2. Bundle Size Optimization (CRITICAL)

3. Server-Side Performance (HIGH)

4. Client-Side Data Fetching (MEDIUM-HIGH)

5. Re-render Optimization (MEDIUM)

6. Rendering Performance (MEDIUM)

7. JavaScript Performance (LOW-MEDIUM)

8. Advanced Patterns (LOW)


How to Use

Read individual rule files for detailed explanations and code examples:

rules/async-parallel.md
rules/bundle-barrel-imports.md
rules/_sections.md

Each rule file contains:


Full Compiled Document

For the complete guide with all rules expanded: AGENTS.md

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.