Subscribe to Derived State
Install this skill
npx skills add vercel-labs/agent-skillsWorks 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
- Identify a component currently tracking a continuously changing raw value
- Extract the conditional logic (e.g., width check) into a dedicated hook
- Replace the raw state dependency with the derived boolean subscriber
- Verify component render frequency using React DevTools profiler
- 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
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.
📄 Full skill instructions — original source: vercel-labs/agent-skills
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)
- Click "Download" above
- In your project, create the directory:
.agent/skills/rerender-derived-state/ - Save the file as
SKILL.md - 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