All skills
vercel-labs avatar

/composition-patterns

@a5343bd official
by Vercel Labsvercel-labs/agent-skills32k stars
2,784

React composition patterns that scale. Use when refactoring components with boolean prop proliferation, building flexible component libraries, or designing reusable APIs. Triggers on tasks involving compound components, render props, context providers, or component architecture. Includes React 19 API changes.

Use this Skill: https://skilld.dev/gh/vercel-labs/agent-skills/composition-patterns

This session only. Nothing lands on disk.

rulesstate-decouple-implementation.md

≈675 tokens on demand. Your agent reads this file only when SKILL.md points to it.

Decouple State Management from UI

The provider component should be the only place that knows how state is managed. UI components consume the context interface—they don't know if state comes from useState, Zustand, or a server sync.

Incorrect (UI coupled to state implementation):

function ChannelComposer({ channelId }: { channelId: string }) {
  // UI component knows about global state implementation
  const state = useGlobalChannelState(channelId)
  const { submit, updateInput } = useChannelSync(channelId)

  return (
    <Composer.Frame>
      <Composer.Input
        value={state.input}
        onChange={(text) => sync.updateInput(text)}
      />
      <Composer.Submit onPress={() => sync.submit()} />
    </Composer.Frame>
  )
}

Correct (state management isolated in provider):

// Provider handles all state management details
function ChannelProvider({
  channelId,
  children,
}: {
  channelId: string
  children: React.ReactNode
}) {
  const { state, update, submit } = useGlobalChannel(channelId)
  const inputRef = useRef(null)

  return (
    <Composer.Provider
      state={state}
      actions={{ update, submit }}
      meta={{ inputRef }}
    >
      {children}
    </Composer.Provider>
  )
}

// UI component only knows about the context interface
function ChannelComposer() {
  return (
    <Composer.Frame>
      <Composer.Header />
      <Composer.Input />
      <Composer.Footer>
        <Composer.Submit />
      </Composer.Footer>
    </Composer.Frame>
  )
}

// Usage
function Channel({ channelId }: { channelId: string }) {
  return (
    <ChannelProvider channelId={channelId}>
      <ChannelComposer />
    </ChannelProvider>
  )
}

Different providers, same UI:

// Local state for ephemeral forms
function ForwardMessageProvider({ children }) {
  const [state, setState] = useState(initialState)
  const forwardMessage = useForwardMessage()

  return (
    <Composer.Provider
      state={state}
      actions={{ update: setState, submit: forwardMessage }}
    >
      {children}
    </Composer.Provider>
  )
}

// Global synced state for channels
function ChannelProvider({ channelId, children }) {
  const { state, update, submit } = useGlobalChannel(channelId)

  return (
    <Composer.Provider state={state} actions={{ update, submit }}>
      {children}
    </Composer.Provider>
  )
}

The same Composer.Input component works with both providers because it only depends on the context interface, not the implementation.

Source: SKILL.md on GitHub

No alerts27d3 checks · Risk SAFE
  • Gen Agent Trust Hub27d

    This skill provides React composition patterns and architectural guidelines for frontend development. No security considerations were identified as the skill consists entirely of instructional documentation.

  • Socket27d

    No alerts

  • Snyk27d

    Risk: LOW · No issues

Signed by skilld at a5343bd. This ties the file your Agent reads to that commit on GitHub. It does not review the instructions.

Last checked against GitHub last month.

Steadyupdated 8 months ago
metadata
{
  "author": "vercel",
  "version": "1.0.0"
}
  • React
  • Refactoring
  • composition
  • component-architecture
  • state-management
  • compound-components
  • react19
  • component-library

README badge

README badge for vercel-labs/agent-skills/composition-patterns

Teaches React composition patterns to avoid boolean prop proliferation and build flexible component libraries using compound components, context providers, and state lifting. Includes guidelines for refactoring component architecture and references React 19 API changes like removing forwardRef in favor of use().

Generated from the current SKILL.md.

Does this skill cover React 19 changes?
Yes. The skill includes a React 19 APIs section covering changes like removing forwardRef in favor of use() and useContext() updates, but those rules only apply if you're using React 19 or later.
What specific composition patterns does this skill teach?
The skill covers compound components, render props, context providers, state lifting, and explicit variants. It prioritizes avoiding boolean prop proliferation by composing components instead.
Is this skill for building my own components or refactoring existing ones?
Both. Use it when refactoring components with too many boolean props, designing new component libraries, or reviewing component architecture decisions.
Does this skill include code examples?
Yes. Individual rule files contain incorrect and correct code examples with explanations for each pattern.

Generated from the current SKILL.md. These answers refresh after source changes.