Community编程与开发github.com

sk-react

构建和修复 React 与 TypeScript 组件,涵盖状态放置、Effect 依赖、数据获取、列表渲染以及导致 UI 卡死或无限循环的重新渲染陷阱。适用于编写或修改 React 组件或 Hook、调试数据过期、无限渲染循环、输入丢失,或审查前端代码时。

sk-react 是什么?

该技能为 AI 助手提供了一套全面的 React 和 TypeScript 开发最佳实践指南。它专注于解决 React 应用中最常见的六大问题领域:状态管理、副作用 (Effect)、数据获取、渲染性能、TypeScript 类型安全以及验证策略。技能文档的核心原则是“每个值应有且只有一个所有者”,强烈建议通过推导而非存储来避免状态不一致。对于副作用,它强调依赖数组必须完整无缺,并需要提供清理函数以防止内存泄漏和竞态条件。在数据获取方面,技能推荐优先使用项目自带的数据库,若手动实现则必须处理加载、空、错误和成功四种状态,并取消过时的请求。文档还提供了关于列表键 (key)、性能优化 (memo/useMemo/useCallback) 以及 TypeScript 类型断言的具体指导。该技能适用于所有支持 Markdown 格式的 AI 编程助手,如 Claude Code、Cursor 或 Codex,能有效提升前端代码质量和开发效率。

兼容平台~Claude Code~Codex CLI~Cursor
Part ofReact
npx skills add https://github.com/vanhuydotcom/sk-skills/tree/HEAD/skills/sk-react

Installed? Explore more 编程与开发 skills: steipete/bluebubbles, steipete/eightctl, steipete/blucli · View all 6 →

在你喜欢的 AI 中提问

打开一个已预加载此 Agent Skill 的新对话。

文档

React 和 TypeScript

将状态放在一处

每个值都应有且只有一个所有者。大多数 React 错误都是两个相同事实的不同副本不一致导致的。

优先推导而非存储。如果一个值可以在渲染时从 props 和 state 计算得出,那就计算它——不要将其镜像到 useState 中,因为副本在其来源改变的那一刻就会过时。const total = items.reduce(...) 不需要 state,也永远不会出错。

从 prop 复制而来的 state 是同一个问题,只是引信更长:useState 的初始值只读取一次,之后便永远忽略。如果组件必须在某个 prop 改变时重置,请给它一个 key,让 React 重新挂载它。

仅将状态提升到需要它的最近的共同父组件。提升得太远会导致每个兄弟组件在每次按键时都重新渲染。

副作用

Effect 是与 React 外部的东西进行同步:订阅、定时器、文档、命令式库。它不是计算值或响应点击的地方——事件处理函数才是。

依赖数组必须列出 effect 读取的所有内容。省略某个值以阻止循环会使其冻结在该值的初始值上,并产生闭包过期 bug。如果 effect 因依赖项是每次渲染都会重建的对象或函数而导致循环,应修复该依赖项——使用 useCallback、useMemo,或将其移出组件——而不是将其从数组中删除。

每当 effect 启动任何可能超出其生命周期的事物时,都应返回一个清理函数。订阅、定时器和进行中的请求都需要在组件卸载或依赖项变化时停止。在开发环境下的 StrictMode 中,effect 会运行两次,专门用于暴露缺失的清理函数。

async 函数不能作为 effect 的主体——它会返回一个 promise,而 React 期望返回的是清理函数。应在 effect 内部声明异步操作并调用它。

数据获取

优先使用项目的数据库,而不是在 effect 中手动编写数据获取逻辑;它已经解决了缓存、去重、重试以及你将要编写的竞态条件。

手动获取数据时,需处理所有四种状态——加载、空、错误、成功——并在清理时取消请求。按顺序发起的两个请求仍然可能以乱序返回结果,因此要么中止前一个请求,要么忽略不再是最新的响应。

绝不能吞掉 reject。处理函数或 effect 中未处理的 reject 会静默失败,UI 会完全停止更新。

渲染

使用数据中的稳定标识作为列表的 key。只有对于从不重新排序、插入或删除的列表,索引 key 才是正确的;当发生上述操作时,React 会复用错误的 DOM 节点,导致输入框保留上一行的值。

在通过性能分析器测量之后,再考虑使用 memo、useMemo 和 useCallback。默认到处使用它们会增加内存分配和容易出错的依赖数组,并且当实际开销是父组件重新渲染整个子树时,它们毫无作用。

绝不要在条件语句、循环或提前 return 之后调用 Hook。每次渲染时 Hook 的调用顺序必须完全相同。

TypeScript

将 any 和类型断言视为未解决的问题,而非修复。两者都在编译器的发言最有价值的地方使其沉默。当类型确实未知时,使用 unknown 并进行类型收窄。

对跨越边界的数据进行类型定义:API 响应、表单值、URL 参数。来自网络的数据并非你所声明的那个类型,除非经过检查,因此在边界处进行验证,让应用的其余部分信任该结果。

验证

构建成功证明的东西比看起来要少。许多项目配置框架忽略构建时的 lint 和类型错误,因此在使用构建结果作为证据之前,请检查配置,并明确运行类型检查和 linter。

然后测试组件:空状态、错误状态、慢速网络,以及在第一次交互完成之前进行的第二次快速交互。

Individual skills in this repo

This repo contains 5 individual skills — each has its own dedicated page.

sk-api

Design and change API contracts (REST, gRPC, protobuf, events) so existing callers keep working, covering resource and method shape, error models, pagination, versioning and the deploy order a breaking change requires. Use when adding or altering an endpoint, message or event, or when judging whether a proposed change breaks a consumer.

sk-code-review

Shows an agent how to design, debug, or review "sk code review" so the concrete output is a code change with a check. The procedure moves through Code review, Establish the diff first, What to look for, in priority order. Use it when a new assignment calls for this craft.

sk-go-service

Implement, extend or debug a handler, service or repository in a Go backend service, covering context propagation, error wrapping, transaction boundaries, tenancy scoping and table-driven tests. Use when writing or changing Go server code, adding an endpoint or gRPC method, touching a repository query, or diagnosing a panic, leak or flaky test in a Go service.

sk-python

Write and debug Python with attention to the traps that produce silent wrong behaviour — mutable defaults, late-binding closures, shallow copies, swallowed exceptions — plus typing, environment and packaging with uv or venv, and pytest structure. Use when writing a Python script, service or library, setting up a Python project, or debugging behaviour that looks impossible.

sk-system-design

Design or evaluate a distributed system from its constraints — service boundaries, synchronous versus event-driven communication, consistency, failure behaviour and capacity — and state the trade-off each choice buys. Use when splitting or merging services, choosing between a call and an event, deciding where data lives, reviewing an architecture proposal, or answering a system design question.

相关技能