跳到正文
Skillpedia
网站与界面(前端)33k · #8793

react-useeffect

它能做什么

这项技能教你的 AI 智能体只在真正需要时才使用 React 的 useEffect。适合 React 代码中有可以更简单的 Effect 的开发者。

适用场景

  • ✓当你在 React 组件中编写或审查 useEffect 时。
  • ✓当你用 useState 和 useEffect 同步状态,而写法显得笨拙时。
  • ✓当你想在 React 中获取数据或重置状态,并寻求最简洁的做法时。

技能链接

https://github.com/davila7/claude-code-templates/blob/main/cli-tool/components/skills/development/react-useeffect/SKILL.md

复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。

使用方法

最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。

可直接粘贴的消息

请用这个技能帮我:https://github.com/davila7/claude-code-templates/blob/main/cli-tool/components/skills/development/react-useeffect/SKILL.md 阅读该链接中的 SKILL.md 文件,并在我的任务中按照它的说明来做。
想让你的 AI 自己找到这样的技能吗?安装免费的 Skillpedia MCP:只需一行,每次任务前它都会在 15,000+ 个技能中搜索。1 分钟完成安装 →

可以这样问 AI

“请帮我审查这个 React 组件,并删除所有不需要的 useEffect 调用。”

每周 5 个新技能,1 分钟读完。

工作原理

智能体会对照一份“不需要 Effect”的情形清单,逐一检查每个 Effect,例如派生值或事件响应。然后用更简单的写法替换,比如在渲染时计算,或使用事件处理函数。

技术内容(原始 SKILL.md)

作者在 GitHub 上的英文原文。

You Might Not Need an Effect

Effects are an escape hatch from React. They let you synchronize with external systems. If there is no external system involved, you shouldn't need an Effect.

Quick Reference

Situation DON'T DO
Derived state from props/state useState + useEffect Calculate during render
Expensive calculations useEffect to cache useMemo
Reset state on prop change useEffect with setState key prop
User event responses useEffect watching state Event handler directly
Notify parent of changes useEffect calling onChange Call in event handler
Fetch data useEffect without cleanup useEffect with cleanup OR framework

When You DO Need Effects

  • Synchronizing with external systems (non-React widgets, browser APIs)
  • Subscriptions to external stores (use useSyncExternalStore when possible)
  • Analytics/logging that runs because component displayed
  • Data fetching with proper cleanup (or use framework's built-in mechanism)

When You DON'T Need Effects

  1. Transforming data for rendering - Calculate at top level, re-runs automatically
  2. Handling user events - Use event handlers, you know exactly what happened
  3. Deriving state - Just compute it: const fullName = firstName + ' ' + lastName
  4. Chaining state updates - Calculate all next state in the event handler

Decision Tree

Need to respond to something?
├── User interaction (click, submit, drag)?
│   └── Use EVENT HANDLER
├── Component appeared on screen?
│   └── Use EFFECT (external sync, analytics)
├── Props/state changed and need derived value?
│   └── CALCULATE DURING RENDER
│       └── Expensive? Use useMemo
└── Need to reset state when prop changes?
    └── Use KEY PROP on component

Detailed Guidance

常见问题

什么时候确实需要 Effect?

当需要与外部系统同步时,例如浏览器 API 或 React 之外的订阅。

重置状态时用什么代替 Effect?

可以使用 key 属性,在 prop 变化时重置组件的状态。

数据获取怎么办?

使用带清理函数的 Effect,或使用框架内置的数据机制。

详情

作者的原始描述

React useEffect best practices from official docs. Use when writing/reviewing useEffect, useState for derived values, data fetching, or state synchronization. Teaches when NOT to use Effect and better alternatives.

repo: davila7/claude-code-templates

热度

GitHub 星标
33k
排名
#8793
许可证
MIT
更新时间
2026/11/2

这个技能有用吗?

0

登录 后即可投票。

评论

  • …

同一仓库的更多技能

相似技能