Skip to content
Skills Library
Programming · Building & coding

refactor-react-effects

/refactor-react-effects

Refactor avoidable React useEffect usage in Langfuse frontend code. Use when adding, reviewing, or removing effects; initializing forms or local UI state from query data; synchronizing client and server state; moving mutations or async workflows out of components; cleaning every effect from a frontend submodule; or reviewing whether an effect has a valid external-system owner.

Repository
langfuse ↗
Licence
MIT
Last repository change
25.10.2026

Assessment and security

100/ 100Class: good
Format25 / 25
Frontmatter with name and description, valid name, description up to 1,024 characters, name matching the folder.
Discoverability25 / 25
Description explains when to use the skill, has a reasonable length and uses third person.
Content35 / 35
At least 150 words and at most 500 lines, headings, step lists, examples or code.
Hygiene15 / 15
No placeholders such as TODO, no Windows paths, with a licence.
Riskno signals detected in the scan
Files in this package
3
Scripts
0
HTML comments
0
URLs in SKILL.md
0
Deprecation

No signals detected. Repository changed on 25.10.2026, without references to retired models or APIs.

Licence

MIT, open: use and modify while retaining author attribution.

What it does and when to use it

Refactor avoidable React useEffect usage in Langfuse frontend code. Use when adding, reviewing, or removing effects; initializing forms or local UI state from query data; synchronizing client and server state; moving mutations or async workflows out of components; cleaning every effect from a frontend submodule; or reviewing whether an effect has a valid external-system owner.

Original author description

Refactor avoidable React useEffect usage in Langfuse frontend code. Use when adding, reviewing, or removing effects; initializing forms or local UI state from query data; synchronizing client and server state; moving mutations or async workflows out of components; cleaning every effect from a frontend submodule; or reviewing whether an effect has a valid external-system owner.

How it works

  1. Required Context
  2. Decision Rules
  3. Fast Smell Test
  4. Workflow
  5. Output Format
Original SKILL.md headings · 152 lines, 905 words
SKILL.md preview
---
name: refactor-react-effects
description: |
  Refactor avoidable React useEffect usage in Langfuse frontend code. Use when
  adding, reviewing, or removing effects; initializing forms or local UI state
  from query data; synchronizing client and server state; moving mutations or
  async workflows out of components; cleaning every effect from a frontend
  submodule; or reviewing whether an effect has a valid external-system owner.
---

# Refactor React Effects

Do not add `useEffect` by default. Use it only to synchronize a component with
a concrete system outside React. If there is no external system, remove the
effect.

Before adding or retaining an effect, answer all of these questions:

1. Which external system is being synchronized?
2. What starts or updates the synchronization?
3. What cleanup, if any, prevents leaks or duplicate subscriptions?
4. Why can an event handler, query API, render derivation, conditional mount,
   or existing integration hook not own the behavior?

If the first question has no concrete answer, do not use an effect.

## Required Context

- Read `web/AGENTS.md` and the nearest package guidance before changing web
  code.
- For large features, local Zustand stores, tables, or controller components,
  also read
  [`../frontend-large-feature-architecture/SKILL.md`](../frontend-large-feature-architecture/SKILL.md).
- Read
  [`references/refactoring-patterns.md`](references/refactoring-patterns.md)
  before implementing a refactor.

## Decision Rules

Apply these rules in order:

1. **Derive during render.** Compute values from current props, query data, and
   client state instead of storing a synchronized copy.
2. **Gate on required data.** If a form or UI cannot initialize correctly
   without query data, let a parent own loading/error handling. Mount a child
   only after the data exists and pass guaranteed initial values to its lazy
   `useState` initializer.
3. **Make reset semantics explicit.** Use a `key` to remount for a new entity
   only when discarding the old local draft is intended. Use an explicit user
   action for refresh/reset when drafts must not be overwritten silently.
4. **Run actions from events.** Put submit, mutation, navigation, notification,
   and multi-step async workflows in event handlers, `actions/*.ts`, or store
   actions. Pass a React Query client, mutation callback, or vanilla Zustand
   store as a dependency; never call React hooks from those actions.
5. **Keep real integration effects.** Subscriptions, observers, browser event
   listeners, timers, and imperative third-party APIs may need an effect. Make
   setup and cleanup symmetrical and keep the effect in a narrow integration
   hook or container. In a direct-effect-free module, use a pre-existing,
   centrally owned integration hook. Do not create a feature-local wrapper just
   to hide `useEffect`.

Presume that an effect which writes React or Zustand state from props or query
data is removable. Do not evade the design problem by switching to
`useLayoutEffect`, suppressing dependency lint, hiding the same synchronization
in a custom hook, or adding an ESLint disable.

## Fast Smell Test

Refactor when any of these shapes appear:

- `useEffect(() => setX(deriveFromY(y)), [y])`;
- fetch data, then mirror it with `setState`;
- set a flag, let an effect perform the action, then clear the flag;
- chain effects where one state write triggers the next effect;
- reset local state because an ID or prop changed;
- guard an integration inside an effect when the component could mount only
  after the precondition is true.

## Workflow

### 1. Establish the Behavior Boundary

Identify the state owner, query owner, user events, external systems, and
loading/error states. For a bug fix, add the failing test first and confirm it
fails. For a behavior-preserving migration, reuse existing coverage where it
protects the relevant behavior; add focused coverage only for a meaningful
behavior risk such as draft preservation, entity changes, refetches, submits,
or cleanup. Do not add tests that inspect source code or merely assert that a
hook is absent. Use behavior tests and the effect inventory instead.

### 2. Inventory Every Effect

Search the full target module, including tests and stories:

```bash
rg -n '\b(use(?:Layout)?Effect|React\.use(?:Layout)?Effect)\b' 'web/src/features/<feature>'
```

Classify every result as:

- render derivation;
- query-to-local-state initialization;
- client/server state synchronization;
- user-event or async workflow;
- external-system integration;
- test-only harness behavior.

Do not refactor from a raw count alone. Record what triggers each effect, what
it writes or controls, and the replacement pattern.

### 3. Refactor One Semantic Seam at a Time

Prefer this order:

1. replace redundant state with pure derivation;
2. split data-owning parents from stateful children;
3. move event-driven work to handlers or external actions;
4. isolate unavoidable integrations in narrow hooks;
5. remove dead state, guards, dependencies, and imports.

Keep user-visible behavior stable. Avoid bundling unrelated file moves, visual
changes, and state architecture changes into the same slice.

### 4. Complete a Submodule Migration

Repeat the inventory after each slice. Before declaring a submodule clean:

- account for every original effect;
- confirm no effect merely moved behind a wrapper hook;
- confirm refetches do not overwrite local edits;
- confirm entity changes have deliberate preserve/reset semantics;
- confirm complex actions are callable without rendering the feature;
- document any real external integration effect that remains outside the
  refactoring target.

Do not add or modify effect-specific ESLint rules or pre-commit checks as part
of this workflow. Treat enforcement policy as separate, explicitly requested
work.

### 5. Verify

Run, at minimum:

- focused client tests for the changed behavior;
- ESLint on the migrated path, then `pnpm --filter web run lint`;
- type checking when state or action interfaces changed;
- a real-browser review for user-visible flows, using seeded data where
  applicable — yours where the outcome is uncertain, otherwise the developer's,
  per `.agents/AGENTS.md`;
- the effect inventory again to prove the intended scope is clean.

Report the exact command summary lines and any retained effects with their
external-system rationale.

## Output Format

Return valid Markdown. For module audits, include a compact table with the
effect location, classification, replacement, and status. Check headings,
lists, links, tables, backticks, and code fences for valid Markdown syntax
before returning the report.
Files in this package (3)
  • SKILL.md 7 KB
  • agents/openai.yaml 1 KB
  • references/refactoring-patterns.md 5 KB

Versions

The database has no other skills named refactor-react-effects.

Database snapshot: 13.11.2026 · package version 69317eedb49a (SHA-256 digest of the entire package)

Downloads in the last 30 days: 0 · ranking

Report a problem with this skill: harmful content, incorrect descriptions or an author’s removal request.

Similar skills

  • 100react-effects-audit
    Use when auditing React or Next.js components for unnecessary or unsafe useEffect usage -- detects 9 anti-patterns from \"You Might Not Need an Effect\".
  • 100no-use-effect
    Prevent unnecessary React `useEffect` usage by steering code toward derived state, event handlers and form Actions, `key`-based resets, `useSyncExternalStore`, `useEffectEvent`, and framework or query-library data APIs. Use when writing or reviewing React/Next.js components,…
  • 100no-use-effect
    Prevent unnecessary React `useEffect` usage by steering code toward derived state, event handlers, memoization, `key`-based resets, `useSyncExternalStore`, and framework or query-library data APIs. Use when writing or reviewing React/Next.js components, refactoring effect-heavy…
  • 100no-use-effect
    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.…
  • 100react-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.
  • 100react-best-practices
    React performance optimization guidelines. Use when writing, reviewing, or refactoring React components, hooks, effects, data fetching, or bundle optimization. Includes useEffect best practices — when NOT to use effects and better alternatives. Triggers: "when writing React…

Feedback and reports

Your message goes to Bartosz Sokoliński. An email address is needed only for a reply. See the privacy policy.