ref-patterns

Guide React ref design for DOM access, forwarding, and imperative APIs.

1|Updated May 6, 2026
One-click install
npx skills add https://github.com/jacob-balslev/skill-graph --skill ref-patterns
Or copy as Structured Prompt for Agent▼
Please help me install this Agent Skill.
Skill: ref-patterns
Source: https://github.com/jacob-balslev/skill-graph/tree/main/marketplace/skills/ref-patterns
Command: npx skills add https://github.com/jacob-balslev/skill-graph --skill ref-patterns

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

This Skill prevents common React ref misuse by helping you choose refs over state when you need a stable mutable handle for DOM access, measurement, focus, animations, third-party integration, or a narrowly-scoped imperative API.

Core Features & Use Cases

  • Ref vs state decision-making: explains when a changing value should drive rendering (state) versus when it should not trigger renders (ref).
  • useRef patterns: covers DOM refs and mutable instance values that persist across renders (e.g., interval ids, latest-args closures, previous-value snapshots).
  • Ref callback patterns: uses ref={(node) => ...} for precise mount/unmount hooks and conditional attachment/composition.
  • Ref propagation across component boundaries: teaches forwardRef (legacy/new options), and React 19 ref-as-prop for passing refs into function components.
  • Controlled imperative APIs: uses useImperativeHandle to expose only the minimal methods a parent truly needs.
  • Compound-component ref forwarding: ensures primitives forward refs through layers (e.g., Radix Slot-style merge) so consumer focus/measurement works.
  • Client/Server boundary constraints: clarifies why refs require Client Components and cannot be created/passed across Server boundaries.

Quick Start

Use the ref-patterns skill to design a React component that forwards a ref safely (including compound-component cases) and uses useImperativeHandle only when imperative control is truly required.

Frequently Asked Questions about ref-patterns

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
When should I use a ref instead of state in React components?▼

Use React refs instead of state when you need a stable mutable handle for DOM access, measurement, or focus that does not trigger re-renders. State drives rendering, while refs persist mutable values silently across render cycles.

How do I forward a ref through a React component to a child DOM element?▼

You forward a ref through React components by using forwardRef for legacy compatibility or the React 19 ref-as-prop feature. This passes the ref directly through component boundaries down to the target DOM node.

What is the best way to expose imperative methods from a child component to a parent in React?▼

The best way to expose imperative methods in React is using useImperativeHandle. This hook exposes a minimal, controlled API from a child component that parents invoke via a ref, keeping internal logic encapsulated.

Why does my ref callback not work when passing a component across Server and Client boundaries?▼

Refs fail across Server and Client boundaries because React requires refs to exist exclusively in Client Components. Passing or creating refs across Server boundaries breaks since server-side code lacks DOM access.

How do I compose multiple refs on a single React component?▼

Composing multiple refs in React uses ref callback patterns to merge forwarded and local refs together. This ensures consumer focus and measurement work correctly through compound-component layers like Radix Slot.