compound-pattern

Implement React compound components that share implicit state via Context API.

1|Updated Apr 3, 2026
One-click install
npx skills add https://github.com/TierOne-Studio/spa-velocity --skill compound-pattern-tierone-studio
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: compound-pattern
Source: https://github.com/TierOne-Studio/spa-velocity/tree/main/.ruler/skills/compound-pattern
Command: npx skills add https://github.com/TierOne-Studio/spa-velocity --skill compound-pattern-tierone-studio

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve? Building related React components like tabs, dropdowns, and menus often leads to prop drilling or duplicated state logic. This Skill teaches the compound component pattern so related components coordinate through shared implicit state without explicit prop passing. ## Core Features & Use Cases - Context-Based State Sharing: Use React Context API to share state between a parent compound component and its children. - Static Sub-Component API: Attach child components as static properties (e.g., FlyOut.Toggle, FlyOut.List) for a clean single-import API. - Alternative Implementation: Covers the React.Children.map with cloneElement approach and its nesting limitations. - Use Case: Build a FlyOut menu where Toggle, List, and Item components coordinate open/close state without the consumer managing any state. ## Quick Start Refactor my tabs component to use the compound component pattern with React Context so the tab list and panels share state implicitly.

Frequently Asked Questions about compound-pattern

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

FAQPage Schema
How do I build a compound component in React?

Create a parent component that holds state and provides it through React Context, then attach child components as static properties like FlyOut.Toggle and FlyOut.List. Children consume the shared state with useContext, so consumers never manage state themselves.

When should I use the compound component pattern?

Use it when building related components like dropdowns, tabs, or menus that share meaningful state and need a clean API without exposing internal state management. Avoid it for simple one-off UIs where a single component with props is clearer.

What is the difference between Context API and React.Children.map for compound components?

Context API lets any nested descendant access shared state, while React.Children.map with cloneElement only passes props to direct children. Cloning also shallow-merges props, which can cause naming collisions with existing props.

Why does my compound component break when I wrap children in a div?

This happens with the React.Children.map approach because only direct children receive the cloned props. Wrapping children in another element cuts off access to open and toggle. Switch to the Context API approach to support flexible nesting.

How do I avoid unnecessary re-renders in compound components?

Memoize the context value passed to the provider so consumers only re-render when the actual state changes. This matters in complex scenarios where the parent re-renders frequently but the shared state stays stable.