Back to Re-render Optimization

Defer State Reads to Usage Point

rerendersearchParamslocalStorageoptimization
28.0k🕒 2026-06-10Source ↗

Install this skill

npx skills add vercel-labs/agent-skills

Works across Claude Code, Cursor, Codex, Copilot & Antigravity

Deferring state reads to the point of usage minimizes unnecessary component re-renders. In React frameworks, hooking into global dynamic sources like search parameters or localStorage at the top level forces a component to re-execute whenever that source changes. By moving these reads into event handlers or specific callbacks, the component remains decoupled from the source's update cycle. This approach keeps the component lifecycle clean and predictable. Instead of listening to every change in the URL query string or browser storage, the application fetches the required value only at the exact moment an action occurs. This practice effectively reduces background noise in the render tree and improves overall responsiveness by preventing layout thrashing and redundant computations that provide no benefit to the user experience in static interface sections.

When to Use This Skill

  • Share buttons relying on current URL query parameters
  • Download triggers that read session-based tokens
  • Analytics tracking buttons that grab current search ref codes
  • Copy-to-clipboard actions reading dynamic browser identifiers

How to Invoke This Skill

Example prompts that trigger this skill in Claude Code, Cursor, or Antigravity:

  • Optimize component re-renders for URL changes
  • Remove unnecessary searchParams subscriptions in my button
  • Stop my component from re-rendering on every query change
  • Move state reads into event handlers for better performance
  • Refactor to read searchParams only on button click

What this skill does

  • Prevents component re-renders triggered by external state changes
  • Decouples event-driven actions from global state listeners
  • Directly accesses browser APIs for instantaneous data retrieval
  • Reduces the surface area for unnecessary background re-calculation
  • Optimizes memory usage by avoiding unnecessary subscription overhead

When not to use it

  • When the component must update immediately as the state changes
  • When the state is needed for the component's UI rendering logic
  • When using libraries that require reactive state subscriptions

Example workflow

  1. Identify a component that re-renders due to top-level state hooks
  2. Remove the reactive state hook from the component body
  3. Define a new event handler function within the component scope
  4. Move the state retrieval logic directly into that handler
  5. Replace the reactive hook reference with a direct API call like URLSearchParams
  6. Verify that the component no longer triggers renders during state updates

Pitfalls & limitations

  • !Direct access to global objects like window can be harder to test
  • !Failing to handle null or undefined results that the original hook might have initialized
  • !May bypass some type-safety wrappers provided by framework-specific hooks

FAQ

Why is reading state at the top level bad for performance?
Reading state at the top level creates a subscription, forcing the component to re-render whenever the underlying state changes, even if the component doesn't display that state.
Does this approach work with all global states?
It is most effective for transient data like URL search parameters or temporary storage, where you only need the value at the moment of an interaction.
What if my component needs to display the search parameter?
If the UI must reflect the current state, you must keep the subscription, though you can use memoization or specific context selectors to limit the impact of those updates.

How it compares

This differs from generic optimization because it focuses on state-subscription management rather than DOM memoization or expensive function calculation, specifically targeting the reactive bridge between the URL and the component lifecycle.

Source & trust

28k stars🕒 Updated 2026-06-10
📄 Full skill instructions — original source: vercel-labs/agent-skills
## 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):**

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):**

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>
}

How to Use This Skill Unit

Option A: Project-Specific (Recommended)

  1. Click "Download" above
  2. In your project, create the directory: .agent/skills/rerender-defer-reads/
  3. Save the file as SKILL.md
  4. The agent will automatically discover the skill based on its description.

Option B: Global Installation (All Agents)

Save the file to these locations to make it available across all projects:

  • Claude Code: ~/.claude/skills/vercel-labs/agent-skills/rerender-defer-reads/SKILL.md
  • Cursor: ~/.cursor/skills/vercel-labs/agent-skills/rerender-defer-reads/SKILL.md
  • Antigravity: ~/.gemini/antigravity/skills/vercel-labs/agent-skills/rerender-defer-reads/SKILL.md

🚀 Install with CLI:
npx skills add vercel-labs/agent-skills

Read the Master Guide: Mastering Agent Skills

Recommended Rules

View more rules

Recommended Workflows

View more workflows

Recommended MCP Servers

View more MCP servers

Take It Further

Maximize your productivity with these powerful resources

📋

Define Your Standards

Set up coding standards to ensure this workflow produces consistent, high-quality results.

Browse Rules Library
📖

Master Workflows

Learn how to create custom workflows, use Turbo Mode, and build your automation library.

Complete Guide

How to use this Skill in Claude Code & Cursor

For Claude Code (CLI)

To use this skill in Claude Code, copy the rule content into your project's custom instructions or follow our Add-Skill CLI guide. This ensures Claude follows your standards during every code generation.

For Cursor & Windsurf

For Cursor or Windsurf, individual skills are best used in the "Rules for AI" section. This specific unit helps the agent avoid re-render optimization issues, leading to cleaner, more efficient code.

Why the skill format matters: the standardized Agent Skills format lets your AI agent load detailed instructions only when they are relevant, keeping your prompt clean while improving results.

Source & attribution

This skill is categorized under Re-render Optimization and is published by Vercel Engineering, maintained in vercel-labs/agent-skills.

← Browse All Agent Skills
Sponsored AI assistant. Recommendations may be paid.