Skip to content
Back to skills

React Compound Pattern

ASecurity

> Build multi-part components that share state implicitly via context

  • 20 stars
  • 0 votes
  • 0 copies
  • 1 view
  • Added September 11, 2026
developmenttypescriptreactnodeapi

Works with

  • cli
  • api

Security analysis

A100/100

Pro scans all 2 files and shows the line behind each finding

Scanned September 11, 2026

npx -y skills add Intense-Visions/harness-engineering --skill react-compound-pattern --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of React Compound Pattern?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for React Compound Pattern
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/intense-visions-react-compound-pattern/badge)](https://www.skillsdirectory.com/skills/intense-visions-react-compound-pattern)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
# React Compound Pattern

> Build multi-part components that share state implicitly via context

## When to Use

- Building UI components with related sub-components (Select/Option, Tabs/Tab/TabPanel, Modal/Header/Body/Footer)
- You want consumers to control composition without prop-drilling
- The parent component needs to coordinate state shared across children
- You are replacing a heavily prop-loaded component with a more flexible API

## Instructions

1. Create a parent component that owns shared state via `useState` or `useReducer`.
2. Create a Context to hold the shared state and expose it.
3. Attach child components as static properties of the parent (`Parent.Child = Child`).
4. Child components read shared state from context — no explicit prop passing required.
5. Export the parent as the public API; children are accessed via dot notation.

```typescript
const FlyOutContext = createContext<{ open: boolean; toggle: () => void } | null>(null);

function FlyOut({ children }: { children: React.ReactNode }) {
  const [open, setOpen] = useState(false);
  return (
    <FlyOutContext.Provider value={{ open, toggle: () => setOpen((o) => !o) }}>
      <div className="flyout">{children}</div>
    </FlyOutContext.Provider>
  );
}

function Toggle() {
  const ctx = useContext(FlyOutContext)!;
  return <button onClick={ctx.toggle}>Toggle</button>;
}

function List({ children }: { children: React.ReactNode }) {
  const ctx = useContext(FlyOutContext)!;
  return ctx.open ? <ul>{children}</ul> : null;
}

FlyOut.Toggle = Toggle;
FlyOut.List = List;
```

## Details

The compound pattern addresses a common pain point: component APIs that grow to have dozens of props as every variant is added. By inverting control to the consumer (they choose the composition), the parent component stays lean and the API stays readable.

**Trade-offs:**

- JSX.Element type inference for attached sub-components requires TypeScript declaration merging or explicit typing
- Deeply nested compound components may need context bridging if the sub-component is used far from the parent
- Over-using this pattern for simple cases adds unnecessary complexity

**Common examples in the wild:**

- HTML `<select>` / `<option>` is the canonical compound component
- Radix UI primitives (Dialog.Root / Dialog.Trigger / Dialog.Content)
- Headless UI (Tab / Tab.Group / Tab.Panel)

## Source

https://patterns.dev/react/compound-pattern

## Process

1. Read the instructions and examples in this document.
2. Apply the patterns to your implementation, adapting to your specific context.
3. Verify your implementation against the details and edge cases listed above.

## Harness Integration

- **Type:** knowledge — this skill is a reference document, not a procedural workflow.
- **No tools or state** — consumed as context by other skills and agents.

## Success Criteria

- The patterns described in this document are applied correctly in the implementation.
- Edge cases and anti-patterns listed in this document are avoided.

Files in this skill

  • SKILL.md3 KB
  • skill.yaml696 B

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

Comments

Loading comments…

Newsletter

Get the weekly list of safe new skills, and the ones we pulled.