react-refs-dom

A set of React rules for refs and direct browser-page manipulation. A ref is a persistent value or reference to a page element that can change without causing a re-render.

In plain words
What is it for?
Use it when working with focus, scrolling, layout measurement, timers, previous values, DOM elements, or non-React widgets.
Why use it?
It helps keep display-changing values in state and prevents unsafe access to page elements during rendering. It also clarifies when cleanup and event-time access are appropriate.

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-refs-dom
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 660 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.00660
Opus 5 $0.00000 $0.00330
Sonnet 5 $0.00000 $0.00132
Haiku 4.5 $0.00000 $0.00066

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

Security

Grade A, and why

react-refs-dom 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-refs-dom.mdc · 66 lines

What it actually says

You are an expert React developer. When generating code that uses refs or manipulates the DOM, follow these rules.

Choosing Between State and Refs

  • Use state when:
    • The value affects what is rendered.
    • A UI update should occur when the value changes.
  • Use refs when:
    • The value does NOT affect the rendered output.
    • You need a mutable container that persists across renders without causing re-renders (e.g., timers, external instances, previous values, DOM nodes).

Accessing DOM Nodes

  • To access a DOM node:
    • Declare a ref: const inputRef = useRef<HTMLInputElement | null>(null).
    • Attach with ref={inputRef}.
    • Read or mutate inputRef.current only in:
      • Event handlers.
      • Effects.
  • Valid DOM ref use cases:
    • Focus management (element.focus()).
    • Scrolling (element.scrollIntoView()).
    • Measuring layout (getBoundingClientRect()).
    • Integrating non-React widgets and imperative libraries.

Rules for Using Refs

  • Do NOT read or write ref.current during render, except for idempotent lazy initialization:
    • Example: if (!ref.current) ref.current = createInstance().
  • Do NOT rely on ref.current changes to trigger UI updates. React does not re-render on ref changes.
  • Treat refs as implementation details:
    • Keep them inside components or custom hooks.
    • Avoid exposing them as primary APIs unless strictly necessary (e.g., forwardRef for focus-able components).

Lists and Collections of DOM Refs

  • Never call useRef inside loops, maps, or conditions.
  • For collections of DOM nodes:
    • Use a single useRef<Map<Key, HTMLElement | null>>(new Map()).
    • Use a ref callback to update the map:
      • On mount: map.set(key, node).
      • On unmount: map.delete(key).

Safe DOM Manipulation

  • Safe operations on DOM nodes owned by React:
    • Focus, scroll, reading layout, adding/removing event listeners.
  • Avoid destructive mutations of DOM nodes that React manages:
    • Do NOT manually remove, reparent, or deeply mutate React-owned elements.
  • When integrating third-party DOM code:
    • Prefer mounting it into a container that React treats as a black box (e.g., an always-empty div).
    • Do all manual DOM tree modifications within that container.

Refs in Custom Hooks

  • Encapsulate DOM-related refs in custom hooks when logic is reusable (e.g., useFocusOnMount, useIntersectionObserver).
  • Custom hooks may:
    • Create and manage refs internally.
    • Return refs to be attached to elements.
  • Keep custom hooks pure with respect to render; side effects must still be inside useEffect within the hook.
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 · 66 lines · 0 tokens per session scan A 2778838d7e35

Subscribe to this mod's changes

react-refs-dom 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 660 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.