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
.claude/rules/rerender-use-ref-transient-values.mdWhat 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.mdThe paths frontmatter field limits the files
AGENTS.md
Codex reads AGENTS.md: paste the rule text there
Manually: put the files in place
.cursor/rules/rerender-use-ref-transient-values.mdcFrontmatter: description, globs, alwaysApply
Manually: put the files in place
.github/instructions/rerender-use-ref-transient-values.instructions.mdThe applyTo field sets which files it applies to
Manually: put the files in place
.devin/rules/rerender-use-ref-transient-values.mdManually: put the files in place
.clinerules/rerender-use-ref-transient-values.mdStep 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