Skip to main content
rule CX Codex

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

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

What 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 searchParams subscription
  • read URL parameters when an event handler runs
  • inspect callback-only localStorage reads
  • avoid rerenders caused by dynamic state subscriptions

Example prompts for the agent

  • Review this component and defer any searchParams read that is used only inside a callback.
  • Rewrite handleShare so ref is read when the user clicks the button.
  • Find callback-only localStorage reads and apply the rerender-defer-reads rule.

How to install rerender-defer-reads

Manually: put the files in place

.claude/rules/rerender-defer-reads.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-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

Similar IDE rules

All IDE rules for Frontend and UI →

From the same set130

The whole set →
bundle-defer-third-party bundle-defer-third-party prevents third-party trackers from inflating the initial bundle. It instructs the assistant to load analytics and monitoring after hydration. CX Codex +1 ★ 7,191repo stars bundle-conditional The bundle-conditional rule recommends loading large modules only when their feature is activated. It applies to lazy-loaded data and browser-only code in SSR builds. CX Codex +1 ★ 7,191repo stars react19-no-forwardref react19-no-forwardref is a Claude Code rule covering React 19 ref and context changes: pass ref as a prop and use use() instead of useContext(). It supports React 19 migrations. CX Codex +1 ★ 7,191repo stars advanced-init-once advanced-init-once prevents running app-wide startup logic inside component useEffect([]) hooks and suggests module-level guards or top-level initialization instead. CX Codex +1 ★ 7,191repo stars rendering-conditional-render The rendering-conditional-render rule prevents accidental rendering of zeros and NaN in JSX. It enforces explicit ternary operators instead of && checks, so falsy values stay out of the DOM. CX Codex +1 ★ 7,191repo stars server-serialization server-serialization is a Claude Code rule for limiting data passed across React Server/Client boundaries. It applies when client components receive props from server components. CX Codex +1 ★ 7,191repo stars rerender-simple-expression-in-memo The rerender-simple-expression-in-memo rule advises against using useMemo for simple expressions that return a boolean, number, or string. It applies when reviewing React component calculations. CX Codex +1 ★ 7,191repo stars js-hoist-regexp js-hoist-regexp — a Claude rule that keeps RegExp construction out of the render path: define the pattern at module scope or wrap it in useMemo(). CX Codex +1 ★ 7,191repo stars

Project sponsors

Companies that keep this analytics open