React Composition
by @wpank
React composition patterns for scalable component architecture. Use when refactoring components with boolean prop proliferation, building flexible component libraries, designing reusable component APIs, or working with compound components and context providers.
clawhub install react-compositionπ About This Skill
name: react-composition model: standard description: React composition patterns for scalable component architecture. Use when refactoring components with boolean prop proliferation, building flexible component libraries, designing reusable component APIs, or working with compound components and context providers. version: "1.0"
React Composition Patterns
Composition patterns for building flexible, maintainable React components. Avoid boolean prop proliferation by using compound components, lifting state, and composing internals. These patterns make codebases easier to work with as they scale.
When to Apply
Pattern Overview
| # | Pattern | Impact | |---|----------------------------|----------| | 1 | Avoid Boolean Props | CRITICAL | | 2 | Compound Components | HIGH | | 3 | Context Interface (DI) | HIGH | | 4 | State Lifting | HIGH | | 5 | Explicit Variants | MEDIUM | | 6 | Children Over Render Props | MEDIUM |
Installation
OpenClaw / Moltbot / Clawbot
npx clawhub@latest install react-composition
1. Avoid Boolean Prop Proliferation
Don't add boolean props like isThread, isEditing, isDMThread to customize
behavior. Each boolean doubles possible states and creates unmaintainable
conditional logic. Use composition instead.
// BAD β boolean props create exponential complexity
function Composer({ isThread, isDMThread, isEditing, isForwarding }: Props) {
return (
)
}// GOOD β composition eliminates conditionals
function ChannelComposer() {
return (
)
}
function ThreadComposer({ channelId }: { channelId: string }) {
return (
)
}
Each variant is explicit about what it renders. Shared internals without a monolithic parent.
2. Compound Components
Structure complex components with shared context. Each subcomponent accesses state via context, not props. Export as a namespace object.
const ComposerContext = createContext(null)function ComposerProvider({ children, state, actions, meta }: ProviderProps) {
return {children}
}
function ComposerInput() {
const { state, actions: { update }, meta: { inputRef } } = use(ComposerContext)
return update((s) => ({ ...s, input: t }))} />
}
const Composer = {
Provider: ComposerProvider, Frame: ComposerFrame,
Input: ComposerInput, Submit: ComposerSubmit, Footer: ComposerFooter,
}
// Consumers compose exactly what they need
3. Generic Context Interface (Dependency Injection)
Define a generic interface with state, actions, and meta. Any provider
implements this contract β enabling the same UI to work with different state
implementations. The provider is the only place that knows how state is managed.
interface ComposerContextValue {
state: { input: string; attachments: Attachment[]; isSubmitting: boolean }
actions: { update: (fn: (s: ComposerState) => ComposerState) => void; submit: () => void }
meta: { inputRef: React.RefObject }
}// Provider A: Local state for ephemeral forms
function ForwardMessageProvider({ children }: { children: React.ReactNode }) {
const [state, setState] = useState(initialState)
return (
{children}
)
}
// Provider B: Global synced state for channels
function ChannelProvider({ channelId, children }: Props) {
const { state, update, submit } = useGlobalChannel(channelId)
return (
{children}
)
}
Swap the provider, keep the UI. Same Composer.Input works with both.
4. Lift State into Providers
Move state into dedicated provider components so sibling components outside the main UI can access and modify state without prop drilling or refs.
// BAD β state trapped inside component; siblings can't access it
function ForwardMessageComposer() {
const [state, setState] = useState(initialState)
return
}
function ForwardMessageDialog() {
return (
)
}// GOOD β state lifted to provider; any descendant can access it
function ForwardMessageProvider({ children }: { children: React.ReactNode }) {
const [state, setState] = useState(initialState)
const submit = useForwardMessage()
return (
{children}
)
}
function ForwardMessageDialog() {
return (
)
}
function ForwardButton() {
const { actions } = use(Composer.Context)
return
}
Key insight: Components that need shared state don't have to be visually nested β they just need to be within the same provider.
5. Explicit Variant Components
Instead of one component with many boolean props, create explicit variants. Each composes the pieces it needs β self-documenting, no impossible states.
// BAD β what does this render?
// GOOD β immediately clear
Each variant is explicit about its provider/state, UI elements, and actions.
6. Children Over Render Props
Use children for composition instead of renderX props. Children are more
readable and compose naturally.
// BAD β render props
}
renderFooter={() => <> >}
/>// GOOD β children composition
When render props are appropriate: When the parent needs to pass data back
(e.g., renderItem={({ item, index }) => ...}).
Decision Guide
1. Component has 3+ boolean props? β Extract explicit variants (1, 5) 2. Component has render props? β Convert to compound components (2, 6) 3. Siblings need shared state? β Lift state to provider (4) 4. Same UI, different data sources? β Generic context interface (3) 5. Building a component library? β Apply all patterns together