react-composition

react-composition

Guide for building scalable React components using composition instead of boolean props. Use when building complex UI components that have multiple variations, when refactoring components with many boolean props (isEditing, isThread, shouldRender*), or when asked to avoid "boolean prop hell". This pattern makes code easier for both humans and AI to work with.

0スター
0フォーク
更新日 1/16/2026
SKILL.md
readonlyread-only
name
react-composition
description

Guide for building scalable React components using composition instead of boolean props. Use when building complex UI components that have multiple variations, when refactoring components with many boolean props (isEditing, isThread, shouldRender*), or when asked to avoid "boolean prop hell". This pattern makes code easier for both humans and AI to work with.

version
"1.0.0"

React Composition Pattern

Avoid boolean prop hell. Use composition to build flexible, maintainable components.

The Problem: Boolean Prop Hell

// ❌ BAD: Monolithic component with boolean props
<UserForm
  isUpdateUser
  isEditNameOnly
  hideWelcomeMessage
  hideTermsAndConditions
  isSlugRequired={false}
  onSuccess={...}
/>

This leads to:

  • Components with 30+ conditions scattered everywhere
  • Hard to understand which props affect which parts
  • Difficult for both humans and AI to modify

The Solution: Composition

Pattern 1: Compound Components (like Radix)

Instead of one monolith with optional props, split into composable parts:

// ✅ GOOD: Compound components
<Composer.Provider>
  <Composer.DropZone />  {/* Only render if needed */}
  <Composer.Frame>
    <Composer.Header />
    <Composer.Input />
    <Composer.Footer>
      <Composer.Actions.PlusMenu />
      <Composer.Actions.Emoji />
      <Composer.Actions.TextFormat />
      <Composer.Submit />
    </Composer.Footer>
  </Composer.Frame>
</Composer.Provider>

Pattern 2: Just Use JSX

Don't pass arrays of actions with boolean flags. Just render what you need:

// ❌ BAD: Array with conditions
const actions = [
  { id: 'plus', hidden: isEditing },
  { id: 'emoji', isMenu: true, menuItems: [...] },
  { id: 'format' },
]

// ✅ GOOD: Just JSX
<Footer>
  {!isEditing && <PlusMenu />}
  <EmojiButton />
  <TextFormatButton />
</Footer>

// ✅ EVEN BETTER: Separate components, no conditions
// In EditMessageComposer.tsx - only render what this variant needs
<Footer>
  <EmojiButton />
  <TextFormatButton />
  <CancelButton />
  <SaveButton />
</Footer>

Pattern 3: Context Provider for State

The Provider defines the interface. Implementation is decided by the consumer:

// Provider defines interface (state + actions)
interface ComposerContext {
  state: { text: string; attachments: File[] }
  actions: { 
    update: (text: string) => void
    submit: () => void 
  }
  meta: { inputRef: RefObject<HTMLInputElement> }
}

// Ephemeral state (forward message modal)
function ForwardMessageProvider({ children }) {
  const [state, setState] = useState({ text: '', attachments: [] })
  return (
    <ComposerContext.Provider value={{
      state,
      actions: { update: setState, submit: handleSubmit },
      meta: { inputRef }
    }}>
      {children}
    </ComposerContext.Provider>
  )
}

// Global synced state (channel composer)
function ChannelProvider({ channelId, children }) {
  const { state, actions } = useGlobalChannel(channelId) // syncs across devices
  return (
    <ComposerContext.Provider value={{ state, actions, meta: { inputRef } }}>
      {children}
    </ComposerContext.Provider>
  )
}

Pattern 4: Lift State Up

When UI outside the component needs access to its state, lift the provider:

// ❌ BAD: Trying to pass state back up
<ForwardMessageModal>
  <Composer onStateChange={setParentState} />
  <ActionBar state={parentState} /> {/* Messy */}
</ForwardMessageModal>

// ✅ GOOD: Lift provider, everything inside can access context
<ForwardMessageProvider>
  <Composer />  {/* Uses context internally */}
  <MessagePreview />  {/* Can also use context */}
  <ActionBar>
    <ForwardButton />  {/* Can call context.actions.submit */}
  </ActionBar>
</ForwardMessageProvider>

The ForwardButton is NOT inside the Composer frame, but it's inside the Provider - so it can access all state and actions.

When to Apply

Use composition when:

  • A component has 3+ boolean props that control rendering
  • The same condition is checked in multiple places within a component
  • Different use cases need different subsets of functionality
  • State needs to be accessed by sibling components

Key Takeaways

  1. No boolean props that determine component trees - If a parent passes isEditing to control what renders, split into separate components instead
  2. Prefer JSX over config arrays - Easier to customize, escape to custom implementations
  3. Provider defines interface, not implementation - Swap state management at the provider level
  4. Lift state when needed - Provider can wrap more than just the "main" component

Reference

Based on Fernando Rojo's talk "Composition Is All You Need" at React Universe Conf 2025.
See references/talk-transcript.md for full context.

You Might Also Like

Related Skills

cache-components

cache-components

137Kdev-frontend

Expert guidance for Next.js Cache Components and Partial Prerendering (PPR). **PROACTIVE ACTIVATION**: Use this skill automatically when working in Next.js projects that have `cacheComponents: true` in their next.config.ts/next.config.js. When this config is detected, proactively apply Cache Components patterns and best practices to all React Server Component implementations. **DETECTION**: At the start of a session in a Next.js project, check for `cacheComponents: true` in next.config. If enabled, this skill's patterns should guide all component authoring, data fetching, and caching decisions. **USE CASES**: Implementing 'use cache' directive, configuring cache lifetimes with cacheLife(), tagging cached data with cacheTag(), invalidating caches with updateTag()/revalidateTag(), optimizing static vs dynamic content boundaries, debugging cache issues, and reviewing Cache Component implementations.

vercel avatarvercel
入手
component-refactoring

component-refactoring

128Kdev-frontend

Refactor high-complexity React components in Dify frontend. Use when `pnpm analyze-component --json` shows complexity > 50 or lineCount > 300, when the user asks for code splitting, hook extraction, or complexity reduction, or when `pnpm analyze-component` warns to refactor before testing; avoid for simple/well-structured components, third-party wrappers, or when the user explicitly wants testing without refactoring.

langgenius avatarlanggenius
入手
web-artifacts-builder

web-artifacts-builder

47Kdev-frontend

Suite of tools for creating elaborate, multi-component claude.ai HTML artifacts using modern frontend web technologies (React, Tailwind CSS, shadcn/ui). Use for complex artifacts requiring state management, routing, or shadcn/ui components - not for simple single-file HTML/JSX artifacts.

anthropics avataranthropics
入手
frontend-design

frontend-design

47Kdev-frontend

Create distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, artifacts, posters, or applications (examples include websites, landing pages, dashboards, React components, HTML/CSS layouts, or when styling/beautifying any web UI). Generates creative, polished code and UI design that avoids generic AI aesthetics.

anthropics avataranthropics
入手
react-modernization

react-modernization

28Kdev-frontend

Upgrade React applications to latest versions, migrate from class components to hooks, and adopt concurrent features. Use when modernizing React codebases, migrating to React Hooks, or upgrading to latest React versions.

wshobson avatarwshobson
入手
tailwind-design-system

tailwind-design-system

28Kdev-frontend

Build scalable design systems with Tailwind CSS v4, design tokens, component libraries, and responsive patterns. Use when creating component libraries, implementing design systems, or standardizing UI patterns.

wshobson avatarwshobson
入手