react-effects-dependencies

A set of React rules for using useEffect, a feature that synchronizes a component with things outside React. It covers dependency lists, event handlers, data fetching, and cleanup.

In plain words
What is it for?
Use it when writing or reviewing effects, subscriptions, browser event listeners, network connections, data fetching, or code that updates state after another update.
Why use it?
It helps prevent extra renders, incomplete cleanup, and effects being used for actions that should happen directly in response to user input.

Cursor rule

Install

Getting it into your agent

One page per mod, every tool's command on it. A separate URL per tool would split the same page into five that compete with each other.

agentmods
npx agentmods add rules/tonyghiani/ai-essentials/react-effects-dependencies
Clone the repo
git clone --depth 1 https://github.com/tonyghiani/ai-essentials
Per session 0 Nothing until a file matches its globs; then the whole rule loads.
When invoked 1,042 The whole file, excluding the scripts and references it only reads on demand.
Security scan A 0 findings. Scan, not verified.
Origin original No closer match found in the catalogue.
Token cost

What it costs to keep this loaded

Counted locally with the o200k_base tokenizer, which is exact for GPT models; Claude uses its own tokenizer and its counts differ. Treat this as one consistent yardstick across the catalogue rather than a bill. Prices are per million input tokens.

ModelPer sessionOnce invoked
Fable 5 $0.00000 $0.01042
Opus 5 $0.00000 $0.00521
Sonnet 5 $0.00000 $0.00208
Haiku 4.5 $0.00000 $0.00104

Measured 2d ago against content hash c7efd4412033, method: parsed. Prices are Anthropic first-party input rates as of 2026-08-30, from the pricing page.

Security

Grade A, and why

react-effects-dependencies scanned grade A with 0 findings against 26 rules in 11 categories — prompt injection, anti-refusal, data exfiltration, privilege escalation, supply chain, agent snooping, system-prompt leakage, SSRF and excessive agency — measured 2d ago.

A static scan of the body, not an audit. Every finding is printed with the line that produced it so you can judge whether it matters here. A mod is markdown that instructs an agent; that is exactly why what it instructs is worth reading.

Nothing flagged

None of the 26 patterns this scan looks for appear in this file: no shell pipes, no recursive deletes, no credential paths, no hidden text, no instruction-override or anti-refusal phrasing, no agent-config snooping. That is not a guarantee, it is the absence of the things that are checkable.

rules/react-effects-dependencies.mdc · 101 lines

How it starts

The opening of the file, as written. The whole thing — 101 lines — stays where its author put it; the contents beside it link to each section on GitHub.

You are an expert React developer. When generating code that uses useEffect and related APIs, follow these rules.

When to Use Event Handlers vs Effects

  • Use event handlers for:
    • Logic that should run only in direct response to user actions (click, submit, keydown, etc.).
    • Sending analytics or POST requests for specific user interactions.
    • Showing toasts or notifications from user actions.
  • Do NOT:
    • Toggle state and then use an effect to "react" to that state change for event-only logic.
    • Chain multiple effects where each only exists to update state for another effect.

When to Use Effects

  • Use effects to synchronize React with external systems:
    • Network connections (websockets, subscriptions, streaming APIs).
    • Browser APIs and event listeners.
    • Non-React widgets and imperative DOM APIs controlled by React state/props.
    • Data fetching tied to "what is currently rendered" when no better framework-level APIs exist.
  • Structure:
    • Setup external synchronization in the effect body.
    • Return a cleanup function that fully undoes the setup.

Effect Purity and Cleanup

  • Effects run after DOM commit, not during render:
    • Never rely on them to compute values needed for immediate rendering.
  • Always provide cleanup for:
    • Subscriptions (unsubscribe/removeEventListener in cleanup).
    • Async tasks (ignore stale results or abort requests in cleanup).
    • External instances (disconnect/destroy on cleanup).
  • Design effects so that:
    • setup → cleanup → setup again is safe and idempotent.
    • React Strict Mode double-mount in development does not break behavior.

Dependencies – Follow the Linter

  • Always include all reactive values used in the effect in the dependency array:
    • Reactive values: props, state, and variables/functions defined in the component body.
  • Treat dependency warnings as errors:
    • Do NOT disable the eslint-plugin-react-hooks rules.
    • Fix the code instead of manually forcing dependency arrays.
  • Values that can be safely omitted from dependencies:
    • State setters returned by useState.
    • The ref objects returned by useRef (NOT ref.current).

Refactoring to Reduce Dependencies

  • To reduce unnecessary dependencies:
    • Move constant, purely static values out of the component into module scope.
    • Move object creation into the effect and depend on primitives instead of objects.
    • Use functional setState updates to avoid capturing stale state:
      • setValue(prev => computeNext(prev, input));
  • Split effects by responsibility:
    • If an effect does multiple unrelated things, break it into multiple effects with focused dependencies.

Avoid Misusing Effects

  • Do NOT:
    • Use effects for derived data; compute derived values during render.
    • Use effects to "fix up" state after render when you can model the desired state directly.
    • Use custom "lifecycle" hooks that hide or avoid dependencies (e.g., useEffectOnce, useMount) to bypass the linter.

Strict Mode Considerations

  • Assume effects can run more than once in development:
    • Never gate the effect with a ref flag just to avoid double execution.
    • Rely on proper cleanup and idempotent setup instead.

Effect Events (useEffectEvent) – If Available

  • When useEffectEvent is available:
    • Use it to define non-reactive callbacks that:
      • Need access to the latest props/state.
      • Should NOT cause the effect to re-run when they change.
  • Pattern:
    • Put subscription setup/teardown in useEffect.
    • Pass the Effect Event to the subscription as the callback.
  • Do NOT:
    • Pass Effect Events as props.
    • Call Effect Events outside an effect context.

Data Fetching with Effects

  • Prefer framework or library-level data APIs (Next.js data fetching, React Server Components, TanStack Query) over manual useEffect fetches.
  • If using useEffect for fetching:
    • Encapsulate logic in a custom hook (e.g., useData, useSearchResults).
    • Handle race conditions:
      • Use an ignore flag or AbortController to discard stale responses.
    • Only re-fetch when relevant parameters (URL, query, page, filters) change.

Read the full file on GitHub · 101 lines

Changes

What this file has done since we first saw it

Hashed on every crawl. A supply-chain change to an agent config is a question of when, not whether, so the history is kept rather than the latest state alone.

  1. 2d ago First seen · 101 lines · 0 tokens per session scan A c7efd4412033

Subscribe to this mod's changes

react-effects-dependencies is a cursor rule published in the GitHub repository tonyghiani/ai-essentials (6 stars, last pushed 24d ago), licensed MIT. It costs nothing until one of its globs matches a file; then it loads 1,042 tokens. A static security scan graded it A with 0 findings. No closer match exists in the catalogue, so it is treated as the original; first seen 2026-08-31.