no-use-effect
它能做什么
这份指南会引导你的智能体远离一种常见的 React 写法,这种写法往往会导致缺陷和多余的渲染。适合编写或审查 React 组件和 Hook 的开发者。
适用场景
- ✓当你的智能体创建新的 React 组件或 Hook 时。
- ✓当它重构现有的 useEffect 调用时。
- ✓当拉取请求新增了一个 useEffect,而你希望对其进行审查时。
技能链接
https://github.com/fastrepl/anarlog/blob/main/.agents/skills/no-use-effect/SKILL.md复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。
使用方法
最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。
可直接粘贴的消息
适用于 Claude Code、Codex 或 Cursor:在项目文件夹中运行,即可安装该技能。
下载 SKILL.md,放进智能体的技能文件夹。
可以这样问 AI
“请审查这个 React 组件,并删除所有并非真正需要的 useEffect。”
每周 5 个新技能,1 分钟读完。
工作原理
这个技能会先询问每个 effect 存在的原因,然后将其替换为最简单且适合的写法。这些写法包括:在渲染过程中计算值、使用数据获取库、在事件处理函数中处理用户操作,以及仅在真正需要一次性初始化时使用的窄 Hook。
技术内容(原始 SKILL.md)
作者在 GitHub 上的英文原文。
No useEffect
Never call useEffect directly unless the work is a true mount-only sync with an external system and no simpler pattern fits. Prefer a render-time computation, a query or mutation hook, an event handler, or a remount boundary instead.
Workflow
- Classify why the effect exists.
- Replace it with the narrowest pattern below.
- Verify with the project's normal lint, typecheck, and tests.
Replacement Patterns
1. Derive state, do not sync it
Replace useEffect(() => setX(derive(y)), [y]) with inline computation or the project's existing memoization conventions.
const filteredProducts = products.filter((product) => product.inStock);
Smell test: state only mirrors props or other state.
2. Use query or mutation libraries for async work
Replace fetch-in-effect code with the app's data layer.
const productQuery = useQuery({
queryKey: ["product", productId],
queryFn: () => fetchProduct(productId),
});
Smell test: the effect fetches, awaits, retries, cancels, or writes async results into local state.
3. Use event handlers for user actions
Do the work where the action happens instead of setting a flag and reacting later.
const handleLike = () => {
postLike();
};
Smell test: state exists only to trigger an effect, then gets reset.
4. Use useMountEffect for mount-only external sync
Use this only for work that is naturally "set up on mount, clean up on unmount": DOM integration, subscriptions, third-party widgets, and stable singleton bindings.
If the project already has a useMountEffect, use it. If not, add a small wrapper:
import { useEffect } from "react";
export function useMountEffect(effect: () => void | (() => void)) {
useEffect(effect, []);
}
Prefer changing component structure so the component only mounts once preconditions are satisfied.
Smell test: the effect is a one-time setup or teardown for something outside React.
5. Use key to reset component state
If a component should behave like a fresh instance when an ID or entity changes, remount it from the parent.
return <VideoPlayer key={videoId} videoId={videoId} />;
Smell test: the effect exists only to reset local state when props change.
Review Standard
Treat new direct useEffect usage as a bug by default. Ask which of the five patterns applies. Accept direct useEffect only with explicit justification that the code is a mount-only external sync and that useMountEffect is the intended escape hatch.
Structure React Code This Way
Order component logic like this:
- Hooks that read external state or data.
- Local state.
- Derived values computed during render.
- Event handlers.
- Early returns.
- Render output.
Do not introduce mirrored state just to make an effect possible.
常见问题
什么时候仍然允许使用 useEffect?
只有在与外部系统进行真正的同步、且仅在挂载时运行一次,并且没有更简单的写法适用时才允许。
它会改变我的应用行为吗?
它力求保持行为不变,指南会要求你之后运行正常的代码检查(lint)、类型检查和测试。
什么是派生值?
在渲染时由其他值计算出来的值,而不是被复制到单独的状态变量中。
详情
作者的原始描述
Avoid direct React `useEffect` usage when writing or reviewing React components and hooks. Use this skill when creating new React code, refactoring existing `useEffect` calls, reviewing PRs that introduce `useEffect`, or when an agent reaches for `useEffect` as a default. Replace effects with derived state, query or mutation libraries, event handlers, `useMountEffect` for mount-only external sync, or `key`-based remounts.
repo: fastrepl/anarlog
热度
- GitHub 星标
- 9.7k
- 排名
- #12213
- 许可证
- MIT
- 更新时间
- 2026/11/1
评论
- …