AGENT SKILL
react-useeffect
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.
- License 许可
- GPL-3.0
- 所属仓库
- Chang-Yo/Dela
- 最近更新
- 2026-11-13
- Stars
- ★ 136
- Forks
- 19
01 前言 Front Matter
- name
- react-useeffect
- description
- 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.
02 资源文件 3
- 参考文档alternatives.md6.0 KB
- 参考文档anti-patterns.md6.0 KB
- 参考文档README.md9.9 KB
03 SKILL.md 正文
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
useSyncExternalStorewhen 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
- Transforming data for rendering - Calculate at top level, re-runs automatically
- Handling user events - Use event handlers, you know exactly what happened
- Deriving state - Just compute it:
const fullName = firstName + ' ' + lastName - 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
- Anti-Patterns - Common mistakes with fixes
- Better Alternatives - useMemo, key prop, lifting state, useSyncExternalStore
04 同仓库更多 Skills
fixture-skill
Fixture skill shipped by a Pi package
★ 136
todo
待办与 Canvas 作业:查询、创建、完成个人待办,跟踪作业提交状态与截止时间
★ 136
shuiyuan
上海交大水源社区(BBS):搜索与浏览话题、读帖子正文、把帖子关联进项目
★ 136
交大邮箱:搜索邮件、读正文与附件、准备邮件草稿
★ 136
document-reading
读取 PDF、Word、PowerPoint 和 Excel 的正文、表格与图片,处理项目文档、Canvas 文件、邮件附件和 web_fetch 返回的 PDF 临时任务。用于理解已有文件,不用于编辑文档或界面预览。
★ 136
curriculum
课表与日程:查询课程安排、创建与管理日历事件
★ 136