Agent Skills › fastrepl/anarlog › no-use-effect

no-use-effect

GitHub

指导在React开发中避免滥用useEffect,通过派生状态、数据查询库或事件处理等模式替代副作用,提升代码质量。

.agents/skills/no-use-effect/SKILL.md fastrepl/anarlog

触发场景

编写新的React组件或Hooks 重构包含useEffect的代码 审查引入useEffect的PR

安装

npx skills add fastrepl/anarlog --skill no-use-effect -g -y
更多选项

非标准路径

npx skills add https://github.com/fastrepl/anarlog/tree/main/.agents/skills/no-use-effect -g -y

不安装直接使用

npx skills use fastrepl/anarlog@no-use-effect

指定 Agent (Claude Code)

npx skills add fastrepl/anarlog --skill no-use-effect -a claude-code -g -y

安装 repo 全部 skill

npx skills add fastrepl/anarlog --all -g -y

预览 repo 内 skill

npx skills add fastrepl/anarlog --list

SKILL.md

Frontmatter
{
    "name": "no-use-effect",
    "description": "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."
}

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

  1. Classify why the effect exists.
  2. Replace it with the narrowest pattern below.
  3. 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:

  1. Hooks that read external state or data.
  2. Local state.
  3. Derived values computed during render.
  4. Event handlers.
  5. Early returns.
  6. Render output.

Do not introduce mirrored state just to make an effect possible.

版本历史

  • 42f3624 当前 2026-09-28 18:50

同 Skill 集合

.agents/skills/add-plugin/SKILL.md
.agents/skills/anarlog-workflow/SKILL.md
.agents/skills/axum-test-transport/SKILL.md
.agents/skills/create-prs/SKILL.md
.agents/skills/fix-prs/SKILL.md
.agents/skills/fix-ready-prs/SKILL.md
.agents/skills/migrate-to-sqlite/SKILL.md
.agents/skills/new-changelog/SKILL.md
.agents/skills/product-update-newsletter/SKILL.md
.agents/skills/qa-cli-mcp-api/SKILL.md
.agents/skills/qa-critical-ux/SKILL.md
.agents/skills/reactive-sqlite-ui/SKILL.md
.agents/skills/release-api/SKILL.md
.agents/skills/release-cli/SKILL.md
.agents/skills/release-docs/SKILL.md
.agents/skills/release-mcp/SKILL.md
.agents/skills/release-new-version/SKILL.md
.agents/skills/release-telemetry/SKILL.md
.agents/skills/sqlite-schema-design/SKILL.md
.agents/skills/sweep-workspace/SKILL.md
.agents/skills/testing/SKILL.md
.agents/skills/update-prs/SKILL.md
.claude/skills/fix-ready-prs/SKILL.md
.claude/skills/product-update-newsletter/SKILL.md
.claude/skills/qa-critical-ux/SKILL.md
.claude/skills/release-new-version/SKILL.md
.cursor/skills/new-changelog/SKILL.md
.cursor/skills/qa-cli-mcp-api/SKILL.md
.cursor/skills/qa-critical-ux/SKILL.md
.cursor/skills/release-new-version/SKILL.md
agent-plugins/anarlog/skills/anarlog/SKILL.md
skills/anarlog/SKILL.md
.agents/skills/gitbutler/SKILL.md

元信息

文件数
0
版本
2660f77
Hash
d1ff8d0f
收录时间
2026-09-28 18:50

首页 - Wiki
Copyright © 2011-2026 iteam. Current version is 2.155.2. UTC+08:00, 2026-11-02 15:44
浙ICP备14020137号-1