Back to Re-render Optimization

Subscribe to Derived State

rerenderderived-statemedia-queryoptimization
28.0k🕒 2026-06-10Source ↗

Install this skill

npx skills add vercel-labs/agent-skills

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

React components often suffer from unnecessary re-renders when they track high-frequency data, such as raw window dimensions or mouse coordinates. The Subscribe to Derived State pattern optimizes performance by isolating the component from the volatile source data. Instead of observing a raw numeric or object value that fluctuates constantly, the component subscribes to a boolean or discrete flag derived from that source. This ensures the component logic only executes when a significant boundary is crossed—for instance, switching from desktop to mobile layout—rather than on every tick of the source data. By offloading the threshold evaluation to a specialized hook like useMediaQuery or a memoized subscriber, the UI remains responsive and avoids expensive DOM reconciliation triggered by irrelevant value shifts.

When to Use This Skill

  • Switching between mobile and desktop navigation menus
  • Enabling or disabling scroll-triggered UI animations
  • Toggling read-only states based on user permission flags
  • Conditional rendering of tooltips during window resize events

How to Invoke This Skill

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

  • Optimize this component to stop re-rendering on window resize
  • How do I prevent unnecessary renders from continuous state updates?
  • Convert this numeric state check into a stable boolean subscription
  • Refactor this hook to only trigger updates at a specific viewport threshold
  • Implement derived state to fix performance lag during layout changes

What this skill does

  • Filters high-frequency data streams into stable boolean toggles
  • Prevents component re-renders during insignificant state mutations
  • Encapsulates threshold logic within reusable effect or state hooks
  • Decouples UI layout concerns from raw input sensitivity
  • Reduces layout thrashing by stabilizing prop updates

When not to use it

  • When the component needs access to the precise numeric value
  • When the derived state changes so frequently that it negates optimization benefits

Example workflow

  1. Identify a component currently tracking a continuously changing raw value
  2. Extract the conditional logic (e.g., width check) into a dedicated hook
  3. Replace the raw state dependency with the derived boolean subscriber
  4. Verify component render frequency using React DevTools profiler
  5. Test that the UI updates correctly only when the threshold state flips

Prerequisites

  • React Hooks
  • Basic knowledge of re-render lifecycles

Pitfalls & limitations

  • !Creating too many derived state hooks can increase initial bundle size
  • !Missing edge cases in boundary conditions might lead to stuck states
  • !Over-optimizing simple components can lead to unnecessary code complexity

FAQ

Why not just use useMemo?
While useMemo stabilizes a value, the component still re-renders if the parent component triggers an update. Subscribing to external state effectively isolates the component from its parent's render lifecycle.
Does this approach work for non-boolean state?
Yes, but it is most effective for booleans or enums that represent discrete states. For complex objects, ensure your subscription includes equality checks to prevent reference-based re-renders.
Will this delay UI updates?
The delay is usually negligible as it happens within the native event loop. It is a trade-off between absolute precision and the performance gain of preventing redundant render cycles.

How it compares

Doing this manually with useEffect often introduces state synchronization bugs; this pattern leverages specialized subscription hooks to handle cleanup and memory management automatically.

Source & trust

28k stars🕒 Updated 2026-06-10
📄 Full skill instructions — original source: vercel-labs/agent-skills
## Subscribe to Derived State

Subscribe to derived boolean state instead of continuous values to reduce re-render frequency.

**Incorrect (re-renders on every pixel change):**

function Sidebar() {
const width = useWindowWidth() // updates continuously
const isMobile = width < 768
return <nav className={isMobile ? 'mobile' : 'desktop'}>
}


**Correct (re-renders only when boolean changes):**

function Sidebar() {
const isMobile = useMediaQuery('(max-width: 767px)')
return <nav className={isMobile ? 'mobile' : 'desktop'}>
}

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-derived-state/
  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-derived-state/SKILL.md
  • Cursor: ~/.cursor/skills/vercel-labs/agent-skills/rerender-derived-state/SKILL.md
  • Antigravity: ~/.gemini/antigravity/skills/vercel-labs/agent-skills/rerender-derived-state/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.