accelint-react-best-practices by gohypergiant/agent-skills
npx skills add https://github.com/gohypergiant/agent-skills --skill accelint-react-best-practices适用于 React 应用程序的全面性能优化和最佳实践,专为处理 React 代码的 AI 智能体和 LLM 设计。
以下是最关键的反模式,它们会导致真实的生产问题。专家们通过艰难的调试会话和性能调查才学到这些教训。
切勿在组件内部定义组件 — 这会在每次渲染时创建新的组件类型,导致完全重新挂载、状态丢失和 DOM 重建。结果是输入字段在按键时失去焦点、动画意外重启,以及 useEffect 的清理/设置函数在每次父组件渲染时运行。
切勿订阅 searchParams/localStorage,如果只在回调函数中读取它们 — 这会导致组件在每次 URL 更改或存储事件时重新渲染,即使组件并不显示这些值。应在回调中直接读取:new URLSearchParams(window.location.search)。
切勿在 useEffect 中使用对象/数组作为依赖项 — 由于每次都会用新引用重新创建对象,这会在每次渲染时触发副作用。应从对象中提取原始值(id、name),并将这些值用作依赖项。
切勿使用 useState + useEffect 同步派生状态 — 这会导致额外的重新渲染、无限循环和陈旧的中间状态。应在渲染期间计算派生值:。
广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
const fullName = firstName + ' ' + lastName切勿在 SSR 组件中直接使用仅限客户端的状态(localStorage、cookies、设备检测) — 这会导致水合不匹配,即服务器 HTML 与客户端渲染不匹配,从而引发 React 警告、视觉闪烁和交互性中断。应在 React 水合之前使用同步的内联 <script>。
切勿在 React 19+ 中使用 forwardRef — 这是已弃用的 API。应改用 ref 作为常规属性:function MyInput({ ref }) { return <input ref={ref} /> }。
切勿将回调函数/对象/数组以内联方式作为属性传递给已记忆化的组件 — 由于每次渲染都会创建新的引用,这会破坏记忆化。应提取到模块作用域、使用 useMemo 或 useCallback:const config = useMemo(() => ({ theme }), [theme])。
切勿将用户交互逻辑放在 useEffect 中 — 如果它是由按钮点击或表单提交触发的,应直接将其放在事件处理程序中。副作用用于与外部系统同步,而非用户触发的操作。
此技能采用渐进式披露结构,以最小化上下文使用:
阅读 AGENTS.md 以获取所有规则的单行摘要概述。
当您识别出相关的优化点时,加载相应的参考文件以获取详细的实现指导:
重新渲染优化:
渲染性能:
高级模式:
其他:
快速参考:
自动化脚本:
每个参考文件包含:
当调用此技能时,请使用标准化的报告格式:
报告格式提供:
何时使用审计模板:
/accelint-react-best-practices <路径> 直接调用技能时何时不使用报告模板:
任务: “当用户滚动时,此组件重新渲染过于频繁”
方法:
任务: “此回调总是使用旧的状态值”
方法:
useCallback 中的陈旧闭包任务: “使用 localStorage 主题时出现水合错误”
方法:
每个参考文件演示一种经过验证的模式,但 React 问题通常有多种有效的解决方案。
应用模式时:
示例: 对于 SSR 水合问题,prevent-hydration-mismatch.md 展示了同步脚本方法,但对于基本用例,简单的“已挂载标志”模式可能更合适。
许多手动优化模式(memo、useMemo、useCallback、提升静态 JSX)由 React 编译器自动处理。
优化前,请检查项目是否使用 React 编译器:
有关编译器处理内容与仍需手动优化内容的完整分解,请参阅 react-compiler-guide.md。
此技能涵盖 React 19 功能,包括:
useEffectEvent (19.2+) 用于稳定的事件处理程序<Activity> 组件用于保留隐藏的组件状态ref 作为属性(取代已弃用的 forwardRef)了解 React 19 功能:
每周安装量
108
代码仓库
GitHub 星标数
7
首次出现
2026年1月30日
安全审计
安装于
codex97
claude-code89
github-copilot85
opencode85
gemini-cli80
cursor76
Comprehensive performance optimization and best practices for React applications, designed for AI agents and LLMs working with React code.
These are the most critical anti-patterns that cause real production issues. Experts learned these the hard way through debugging sessions and performance investigations.
NEVER define components inside components — creates new component type on every render, causing full remount with state loss and DOM recreation. Results in input fields losing focus on keystroke, animations restarting unexpectedly, and useEffect cleanup/setup running on every parent render.
NEVER subscribe to searchParams/localStorage if you only read them in callbacks — causes component to re-render on every URL change or storage event even when the component doesn't display those values. Read directly in the callback instead: new URLSearchParams(window.location.search).
NEVER use object/array dependencies in useEffect — triggers effect on every render since objects are recreated with new references each time. Extract primitive values (id, name) from objects and use those as dependencies instead.
NEVER sync derived state with useState + useEffect — leads to extra re-renders, infinite loops, and stale intermediate states. Calculate derived values during render instead: const fullName = firstName + ' ' + lastName.
NEVER use client-only state (localStorage, cookies, device detection) directly in SSR components — causes hydration mismatches where server HTML doesn't match client render, resulting in React warnings, visual flickering, and broken interactivity. Use synchronous inline <script> before React hydrates.
NEVER use forwardRef in React 19+ — deprecated API. Use ref as a regular prop instead: function MyInput({ ref }) { return <input ref={ref} /> }.
NEVER create callbacks/objects/arrays inline as props to memoized components — breaks memoization since new reference is created each render. Extract to module scope, useMemo, or useCallback: const config = useMemo(() => ({ theme }), [theme]).
NEVER put user interaction logic in useEffect — if it's triggered by a button click or form submit, put it directly in the event handler. Effects are for synchronization with external systems, not user-triggered actions.
This skill uses a progressive disclosure structure to minimize context usage:
Read AGENTS.md for a concise overview of all rules with one-line summaries.
When you identify a relevant optimization, load the corresponding reference file for detailed implementation guidance:
Re-render Optimizations:
Rendering Performance:
Advanced Patterns:
Misc:
Quick References:
Automation Scripts:
Each reference file contains:
When this skill is invoked, use the standardized report format:
Template: assets/output-report-template.md
The report format provides:
When to use the audit template:
/accelint-react-best-practices <path>When NOT to use the report template:
Task: "This component re-renders too frequently when the user scrolls"
Approach:
Task: "This callback always uses the old state value"
Approach:
Task: "Getting hydration errors with localStorage theme"
Approach:
Each reference file demonstrates ONE proven pattern, but React problems often have multiple valid solutions.
When applying patterns:
Example: For SSR hydration issues, prevent-hydration-mismatch.md shows the synchronous script approach, but a simple "mounted flag" pattern may be more appropriate for basic use cases.
Many manual optimization patterns (memo, useMemo, useCallback, hoisting static JSX) are automatically handled by React Compiler.
Before optimizing, check if the project uses React Compiler:
See react-compiler-guide.md for a complete breakdown of what the compiler handles vs what still needs manual optimization.
This skill covers React 19 features including:
useEffectEvent (19.2+) for stable event handlers<Activity> component for preserving hidden component stateref as a prop (replaces deprecated forwardRef)Catch up on React 19 features:
Weekly Installs
108
Repository
GitHub Stars
7
First Seen
Jan 30, 2026
Security Audits
Gen Agent Trust HubPassSocketPassSnykPass
Installed on
codex97
claude-code89
github-copilot85
opencode85
gemini-cli80
cursor76
Genkit JS 开发指南:AI 应用构建、错误排查与最佳实践
6,100 周安装