Skip to main content
rule CX Codex

rerender-use-ref-transient-values — Claude Code rule

The rerender-use-ref-transient-values rule teaches the agent to store frequently changing values in useRef instead of useState to avoid re-rendering the component on every update.

MIT ★ 7,191 updated 2026-10-24

Install into Claude Code · Manually: put the files in place All methods and platforms ↓
.claude/rules/rerender-use-ref-transient-values.md
Open on GitHub

What rerender-use-ref-transient-values does

When a value changes often — mouse coordinates, interval counters, transient flags — updating via useState triggers a component re-render on every change. The rule directs the agent to use useRef for such values: updating .current does not trigger a render, keeping the component fast.

In the rule's example, a mouse tracker using useState re-renders on every cursor move, while the useRef version directly updates the DOM element's transform via dotRef.current.style — no extra renders. The rule's impact is marked as medium: it eliminates unnecessary re-renders on frequent updates.

The rule applies to React components. The agent will choose useRef for values needed inside handlers or effects but not affecting the rendered UI.

What it helps with

  • avoid component re-render on frequent value updates
  • store mouse coordinates or counters without triggering render
  • update DOM directly from useEffect without state trigger

Example prompts for the agent

  • Build a mouse tracker that updates element position without re-rendering the component
  • Store the latest timer value in a ref so the component doesn't render every second
  • Rewrite this counter to use useRef and remove extra renders

How to install rerender-use-ref-transient-values

Manually: put the files in place

.claude/rules/rerender-use-ref-transient-values.md

The paths frontmatter field limits the files

Step by step, with commands for each platform: AGENTS.md vs CLAUDE.md: what they are and which one to use

Questions about rerender-use-ref-transient-values

How to install rerender-use-ref-transient-values in Claude Code?

Place the rule file at .claude/rules/rerender-use-ref-transient-values.md in your project root. Claude Code will pick it up automatically.

What does the rerender-use-ref-transient-values rule do?

It teaches the agent to store frequently changing values in useRef instead of useState to avoid re-rendering the component on every update. Updating .current does not cause a render.

When to use useRef instead of useState per this rule?

When a value changes often (mouse coordinates, intervals, transient flags) and does not directly affect JSX. Keep useState for UI state.

Source text

rerender-use-ref-transient-values.md · MIT
---
title: Use useRef for Transient Values
impact: MEDIUM
impactDescription: avoids unnecessary re-renders on frequent updates
tags: rerender, useref, state, performance
---

## Use useRef for Transient Values

When a value changes frequently and you don't want a re-render on every update (e.g., mouse trackers, intervals, transient flags), store it in `useRef` instead of `useState`. Keep component state for UI; use refs for temporary DOM-adjacent values. Updating a ref does not trigger a re-render.

**Incorrect (renders every update):**

```tsx
function Tracker() {
  const [lastX, setLastX] = useState(0)

  useEffect(() => {
    const onMove = (e: MouseEvent) => setLastX(e.clientX)
    window.addEventListener('mousemove', onMove)
    return () => window.removeEventListener('mousemove', onMove)
  }, [])

  return (
    <div
      style={{
        position: 'fixed',
        top: 0,
        left: lastX,
        width: 8,
        height: 8,
        background: 'black',
      }}
    />
  )
}
```

**Correct (no re-render for tracking):**

```tsx
function Tracker() {
  const lastXRef = useRef(0)
  const dotRef = useRef<HTMLDivElement>(null)

  useEffect(() => {
    const onMove = (e: MouseEvent) => {
      lastXRef.current = e.clientX
      const node = dotRef.current
      if (node) {
        node.style.transform = `translateX(${e.clientX}px)`
      }
    }
    window.addEventListener('mousemove', onMove)
    return () => window.removeEventListener('mousemove', onMove)
  }, [])

  return (
    <div
      ref={dotRef}
      style={{
        position: 'fixed',
        top: 0,
        left: 0,
        width: 8,
        height: 8,
        background: 'black',
        transform: 'translateX(0px)',
      }}
    />
  )
}
```

© tech-leads-club · MIT · tech-leads-club/agent-skills

Similar IDE rules

All IDE rules for Frontend and UI →

From the same set130

The whole set →
server-parallel-fetching The server-parallel-fetching rule shows how to compose React Server Components so independent data requests can run in parallel. Use it when one component waits on another component’s fetch. CX Codex +1 ★ 7,191repo stars _sections Defines eight performance optimization sections, ranks them by impact, and guides the agent in prioritizing code improvements. Install once, reference by ID when analyzing bottlenecks. CX Codex +1 ★ 7,191repo stars client-passive-event-listeners client-passive-event-listeners instructs the agent to add { passive: true } to touch and wheel listeners, preventing scroll stutter in browser viewports. CX Codex +1 ★ 7,191repo stars js-cache-storage js-cache-storage keeps localStorage, sessionStorage and cookie values in memory so reads stop hitting synchronous browser storage. CX Codex +1 ★ 7,191repo stars js-length-check-first js-length-check-first — a rule for Claude Code that prompts the AI to check array lengths before running expensive operations like sorting or deep equality checks. If lengths differ, arrays cannot be equal—no expensive work needed. CX Codex +1 ★ 7,191repo stars js-tosorted-immutable The js-tosorted-immutable rule replaces mutating .sort() with .toSorted(), keeping React props and state arrays unchanged during sorting. CX Codex +1 ★ 7,191repo stars patterns-children-over-render-props patterns-children-over-render-props — a Claude rule that pushes React components toward composition with children instead of renderX props, so call sites read as plain JSX. CX Codex +1 ★ 7,191repo stars js-cache-function-results js-cache-function-results instructs the agent to cache repeated function calls using a module-level Map. It eliminates redundant computations across renders and utility calls. CX Codex +1 ★ 7,191repo stars

Project sponsors

Companies that keep this analytics open