rerender-defer-reads — Claude Code rule: defer state reads
The rerender-defer-reads rule keeps callback-only reads of dynamic state out of component subscriptions. Use it when searchParams or localStorage is needed only after an action.
MIT ★ 7,191 updated 2026-10-24
.claude/rules/rerender-defer-reads.mdWhat rerender-defer-reads does
The rule targets cases where a component reads searchParams or localStorage only from an event handler. It tells the agent to avoid subscribing during render and fetch the value when the callback runs.
Its example replaces useSearchParams() with URLSearchParams(window.location.search) inside handleShare. The value is then used for ref when the button is clicked, rather than being kept as subscribed component state.
useSearchParams() subscribes the component to every searchParams change, so this pattern can cause extra rerenders.
What it helps with
- remove an unnecessary
searchParamssubscription - read URL parameters when an event handler runs
- inspect callback-only
localStoragereads - avoid rerenders caused by dynamic state subscriptions
Example prompts for the agent
- Review this component and defer any
searchParamsread that is used only inside a callback. - Rewrite
handleSharesorefis read when the user clicks the button. - Find callback-only
localStoragereads and apply thererender-defer-readsrule.
How to install rerender-defer-reads
Manually: put the files in place
.claude/rules/rerender-defer-reads.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-defer-reads.mdcFrontmatter: description, globs, alwaysApply
Manually: put the files in place
.github/instructions/rerender-defer-reads.instructions.mdThe applyTo field sets which files it applies to
Manually: put the files in place
.devin/rules/rerender-defer-reads.mdManually: put the files in place
.clinerules/rerender-defer-reads.mdStep by step, with commands for each platform: AGENTS.md vs CLAUDE.md: what they are and which one to use
Questions about rerender-defer-reads
How do I install rerender-defer-reads in Claude Code?
Add the rule file at .claude/rules/rerender-defer-reads.md.
What does rerender-defer-reads do?
It tells the agent to read dynamic state at the point of use when that state is needed only inside a callback, avoiding an unnecessary subscription.
Why avoid useSearchParams() in this case?
It subscribes the component to searchParams changes. Reading window.location.search inside the callback avoids that subscription when the value is not needed for rendering.
Source text
rerender-defer-reads.md · MIT
---
title: Defer State Reads to Usage Point
impact: MEDIUM
impactDescription: avoids unnecessary subscriptions
tags: rerender, searchParams, localStorage, optimization
---
## Defer State Reads to Usage Point
Don't subscribe to dynamic state (searchParams, localStorage) if you only read it inside callbacks.
**Incorrect (subscribes to all searchParams changes):**
```tsx
function ShareButton({ chatId }: { chatId: string }) {
const searchParams = useSearchParams()
const handleShare = () => {
const ref = searchParams.get('ref')
shareChat(chatId, { ref })
}
return <button onClick={handleShare}>Share</button>
}
```
**Correct (reads on demand, no subscription):**
```tsx
function ShareButton({ chatId }: { chatId: string }) {
const handleShare = () => {
const params = new URLSearchParams(window.location.search)
const ref = params.get('ref')
shareChat(chatId, { ref })
}
return <button onClick={handleShare}>Share</button>
}
```
© tech-leads-club · MIT · tech-leads-club/agent-skills