Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Apply React composition patterns to build flexible, maintainable components without boolean prop sprawl
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
AGENTS.md
1# React Composition Patterns23**Version 1.0.0**4Engineering5January 202667> **Note:**8> This document is mainly for agents and LLMs to follow when maintaining,9> generating, or refactoring React codebases using composition. Humans10> may also find it useful, but guidance here is optimized for automation11> and consistency by AI-assisted workflows.1213---1415## Abstract1617Composition 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 for both humans and AI agents to work with as they scale.1819---2021## Table of Contents22231. [Component Architecture](#1-component-architecture) — **HIGH**24- 1.1 [Avoid Boolean Prop Proliferation](#11-avoid-boolean-prop-proliferation)25- 1.2 [Use Compound Components](#12-use-compound-components)262. [State Management](#2-state-management) — **MEDIUM**27- 2.1 [Decouple State Management from UI](#21-decouple-state-management-from-ui)28- 2.2 [Define Generic Context Interfaces for Dependency Injection](#22-define-generic-context-interfaces-for-dependency-injection)29- 2.3 [Lift State into Provider Components](#23-lift-state-into-provider-components)303. [Implementation Patterns](#3-implementation-patterns) — **MEDIUM**31- 3.1 [Create Explicit Component Variants](#31-create-explicit-component-variants)32- 3.2 [Prefer Composing Children Over Render Props](#32-prefer-composing-children-over-render-props)334. [React 19 APIs](#4-react-19-apis) — **MEDIUM**34- 4.1 [React 19 API Changes](#41-react-19-api-changes)3536---3738## 1. Component Architecture3940**Impact: HIGH**4142Fundamental patterns for structuring components to avoid prop43proliferation and enable flexible composition.4445### 1.1 Avoid Boolean Prop Proliferation4647**Impact: CRITICAL (prevents unmaintainable component variants)**4849Don't add boolean props like `isThread`, `isEditing`, `isDMThread` to customize5051component behavior. Each boolean doubles possible states and creates5253unmaintainable conditional logic. Use composition instead.5455**Incorrect: boolean props create exponential complexity**5657```tsx58function Composer({59onSubmit,60isThread,61channelId,62isDMThread,63dmId,64isEditing,65isForwarding,66}: Props) {67return (68<form>69<Header />70<Input />71{isDMThread ? (72<AlsoSendToDMField id={dmId} />73) : isThread ? (74<AlsoSendToChannelField id={channelId} />75) : null}76{isEditing ? (77<EditActions />78) : isForwarding ? (79<ForwardActions />80) : (81<DefaultActions />82)}83<Footer onSubmit={onSubmit} />84</form>85)86}87```8889**Correct: composition eliminates conditionals**9091```tsx92// Channel composer93function ChannelComposer() {94return (95<Composer.Frame>96<Composer.Header />97<Composer.Input />98<Composer.Footer>99<Composer.Attachments />100<Composer.Formatting />101<Composer.Emojis />102<Composer.Submit />103</Composer.Footer>104</Composer.Frame>105)106}107108// Thread composer - adds "also send to channel" field109function ThreadComposer({ channelId }: { channelId: string }) {110return (111<Composer.Frame>112<Composer.Header />113<Composer.Input />114<AlsoSendToChannelField id={channelId} />115<Composer.Footer>116<Composer.Formatting />117<Composer.Emojis />118<Composer.Submit />119</Composer.Footer>120</Composer.Frame>121)122}123124// Edit composer - different footer actions125function EditComposer() {126return (127<Composer.Frame>128<Composer.Input />129<Composer.Footer>130<Composer.Formatting />131<Composer.Emojis />132<Composer.CancelEdit />133<Composer.SaveEdit />134</Composer.Footer>135</Composer.Frame>136)137}138```139140Each variant is explicit about what it renders. We can share internals without141142sharing a single monolithic parent.143144### 1.2 Use Compound Components145146**Impact: HIGH (enables flexible composition without prop drilling)**147148Structure complex components as compound components with a shared context. Each149150subcomponent accesses shared state via context, not props. Consumers compose the151152pieces they need.153154**Incorrect: monolithic component with render props**155156```tsx157function Composer({158renderHeader,159renderFooter,160renderActions,161showAttachments,162showFormatting,163showEmojis,164}: Props) {165return (166<form>167{renderHeader?.()}168<Input />169{showAttachments && <Attachments />}170{renderFooter ? (171renderFooter()172) : (173<Footer>174{showFormatting && <Formatting />}175{showEmojis && <Emojis />}176{renderActions?.()}177</Footer>178)}179</form>180)181}182```183184**Correct: compound components with shared context**185186```tsx187const ComposerContext = createContext<ComposerContextValue | null>(null)188189function ComposerProvider({ children, state, actions, meta }: ProviderProps) {190return (191<ComposerContext value={{ state, actions, meta }}>192{children}193</ComposerContext>194)195}196197function ComposerFrame({ children }: { children: React.ReactNode }) {198return <form>{children}</form>199}200201function ComposerInput() {202const {203state,204actions: { update },205meta: { inputRef },206} = use(ComposerContext)207return (208<TextInput209ref={inputRef}210value={state.input}211onChangeText={(text) => update((s) => ({ ...s, input: text }))}212/>213)214}215216function ComposerSubmit() {217const {218actions: { submit },219} = use(ComposerContext)220return <Button onPress={submit}>Send</Button>221}222223// Export as compound component224const Composer = {225Provider: ComposerProvider,226Frame: ComposerFrame,227Input: ComposerInput,228Submit: ComposerSubmit,229Header: ComposerHeader,230Footer: ComposerFooter,231Attachments: ComposerAttachments,232Formatting: ComposerFormatting,233Emojis: ComposerEmojis,234}235```236237**Usage:**238239```tsx240<Composer.Provider state={state} actions={actions} meta={meta}>241<Composer.Frame>242<Composer.Header />243<Composer.Input />244<Composer.Footer>245<Composer.Formatting />246<Composer.Submit />247</Composer.Footer>248</Composer.Frame>249</Composer.Provider>250```251252Consumers explicitly compose exactly what they need. No hidden conditionals. And the state, actions and meta are dependency-injected by a parent provider, allowing multiple usages of the same component structure.253254---255256## 2. State Management257258**Impact: MEDIUM**259260Patterns for lifting state and managing shared context across261composed components.262263### 2.1 Decouple State Management from UI264265**Impact: MEDIUM (enables swapping state implementations without changing UI)**266267The provider component should be the only place that knows how state is managed.268269UI components consume the context interface—they don't know if state comes from270271useState, Zustand, or a server sync.272273**Incorrect: UI coupled to state implementation**274275```tsx276function ChannelComposer({ channelId }: { channelId: string }) {277// UI component knows about global state implementation278const state = useGlobalChannelState(channelId)279const { submit, updateInput } = useChannelSync(channelId)280281return (282<Composer.Frame>283<Composer.Input284value={state.input}285onChange={(text) => sync.updateInput(text)}286/>287<Composer.Submit onPress={() => sync.submit()} />288</Composer.Frame>289)290}291```292293**Correct: state management isolated in provider**294295```tsx296// Provider handles all state management details297function ChannelProvider({298channelId,299children,300}: {301channelId: string302children: React.ReactNode303}) {304const { state, update, submit } = useGlobalChannel(channelId)305const inputRef = useRef(null)306307return (308<Composer.Provider309state={state}310actions={{ update, submit }}311meta={{ inputRef }}312>313{children}314</Composer.Provider>315)316}317318// UI component only knows about the context interface319function ChannelComposer() {320return (321<Composer.Frame>322<Composer.Header />323<Composer.Input />324<Composer.Footer>325<Composer.Submit />326</Composer.Footer>327</Composer.Frame>328)329}330331// Usage332function Channel({ channelId }: { channelId: string }) {333return (334<ChannelProvider channelId={channelId}>335<ChannelComposer />336</ChannelProvider>337)338}339```340341**Different providers, same UI:**342343```tsx344// Local state for ephemeral forms345function ForwardMessageProvider({ children }) {346const [state, setState] = useState(initialState)347const forwardMessage = useForwardMessage()348349return (350<Composer.Provider351state={state}352actions={{ update: setState, submit: forwardMessage }}353>354{children}355</Composer.Provider>356)357}358359// Global synced state for channels360function ChannelProvider({ channelId, children }) {361const { state, update, submit } = useGlobalChannel(channelId)362363return (364<Composer.Provider state={state} actions={{ update, submit }}>365{children}366</Composer.Provider>367)368}369```370371The same `Composer.Input` component works with both providers because it only372373depends on the context interface, not the implementation.374375### 2.2 Define Generic Context Interfaces for Dependency Injection376377**Impact: HIGH (enables dependency-injectable state across use-cases)**378379Define a **generic interface** for your component context with three parts:380381`state`, `actions`, and `meta`. This interface is a contract that any provider382383can implement—enabling the same UI components to work with completely different384385state implementations.386387**Core principle:** Lift state, compose internals, make state388389dependency-injectable.390391**Incorrect: UI coupled to specific state implementation**392393```tsx394function ComposerInput() {395// Tightly coupled to a specific hook396const { input, setInput } = useChannelComposerState()397return <TextInput value={input} onChangeText={setInput} />398}399```400401**Correct: generic interface enables dependency injection**402403```tsx404// Define a GENERIC interface that any provider can implement405interface ComposerState {406input: string407attachments: Attachment[]408isSubmitting: boolean409}410411interface ComposerActions {412update: (updater: (state: ComposerState) => ComposerState) => void413submit: () => void414}415416interface ComposerMeta {417inputRef: React.RefObject<TextInput>418}419420interface ComposerContextValue {421state: ComposerState422actions: ComposerActions423meta: ComposerMeta424}425426const ComposerContext = createContext<ComposerContextValue | null>(null)427```428429**UI components consume the interface, not the implementation:**430431```tsx432function ComposerInput() {433const {434state,435actions: { update },436meta,437} = use(ComposerContext)438439// This component works with ANY provider that implements the interface440return (441<TextInput442ref={meta.inputRef}443value={state.input}444onChangeText={(text) => update((s) => ({ ...s, input: text }))}445/>446)447}448```449450**Different providers implement the same interface:**451452```tsx453// Provider A: Local state for ephemeral forms454function ForwardMessageProvider({ children }: { children: React.ReactNode }) {455const [state, setState] = useState(initialState)456const inputRef = useRef(null)457const submit = useForwardMessage()458459return (460<ComposerContext461value={{462state,463actions: { update: setState, submit },464meta: { inputRef },465}}466>467{children}468</ComposerContext>469)470}471472// Provider B: Global synced state for channels473function ChannelProvider({ channelId, children }: Props) {474const { state, update, submit } = useGlobalChannel(channelId)475const inputRef = useRef(null)476477return (478<ComposerContext479value={{480state,481actions: { update, submit },482meta: { inputRef },483}}484>485{children}486</ComposerContext>487)488}489```490491**The same composed UI works with both:**492493```tsx494// Works with ForwardMessageProvider (local state)495<ForwardMessageProvider>496<Composer.Frame>497<Composer.Input />498<Composer.Submit />499</Composer.Frame>500</ForwardMessageProvider>501502// Works with ChannelProvider (global synced state)503<ChannelProvider channelId="abc">504<Composer.Frame>505<Composer.Input />506<Composer.Submit />507</Composer.Frame>508</ChannelProvider>509```510511**Custom UI outside the component can access state and actions:**512513```tsx514function ForwardMessageDialog() {515return (516<ForwardMessageProvider>517<Dialog>518{/* The composer UI */}519<Composer.Frame>520<Composer.Input placeholder="Add a message, if you'd like." />521<Composer.Footer>522<Composer.Formatting />523<Composer.Emojis />524</Composer.Footer>525</Composer.Frame>526527{/* Custom UI OUTSIDE the composer, but INSIDE the provider */}528<MessagePreview />529530{/* Actions at the bottom of the dialog */}531<DialogActions>532<CancelButton />533<ForwardButton />534</DialogActions>535</Dialog>536</ForwardMessageProvider>537)538}539540// This button lives OUTSIDE Composer.Frame but can still submit based on its context!541function ForwardButton() {542const {543actions: { submit },544} = use(ComposerContext)545return <Button onPress={submit}>Forward</Button>546}547548// This preview lives OUTSIDE Composer.Frame but can read composer's state!549function MessagePreview() {550const { state } = use(ComposerContext)551return <Preview message={state.input} attachments={state.attachments} />552}553```554555The provider boundary is what matters—not the visual nesting. Components that556557need shared state don't have to be inside the `Composer.Frame`. They just need558559to be within the provider.560561The `ForwardButton` and `MessagePreview` are not visually inside the composer562563box, but they can still access its state and actions. This is the power of564565lifting state into providers.566567The UI is reusable bits you compose together. The state is dependency-injected568569by the provider. Swap the provider, keep the UI.570571### 2.3 Lift State into Provider Components572573**Impact: HIGH (enables state sharing outside component boundaries)**574575Move state management into dedicated provider components. This allows sibling576577components outside the main UI to access and modify state without prop drilling578579or awkward refs.580581**Incorrect: state trapped inside component**582583```tsx584function ForwardMessageComposer() {585const [state, setState] = useState(initialState)586const forwardMessage = useForwardMessage()587588return (589<Composer.Frame>590<Composer.Input />591<Composer.Footer />592</Composer.Frame>593)594}595596// Problem: How does this button access composer state?597function ForwardMessageDialog() {598return (599<Dialog>600<ForwardMessageComposer />601<MessagePreview /> {/* Needs composer state */}602<DialogActions>603<CancelButton />604<ForwardButton /> {/* Needs to call submit */}605</DialogActions>606</Dialog>607)608}609```610611**Incorrect: useEffect to sync state up**612613```tsx614function ForwardMessageDialog() {615const [input, setInput] = useState('')616return (617<Dialog>618<ForwardMessageComposer onInputChange={setInput} />619<MessagePreview input={input} />620</Dialog>621)622}623624function ForwardMessageComposer({ onInputChange }) {625const [state, setState] = useState(initialState)626useEffect(() => {627onInputChange(state.input) // Sync on every change 😬628}, [state.input])629}630```631632**Incorrect: reading state from ref on submit**633634```tsx635function ForwardMessageDialog() {636const stateRef = useRef(null)637return (638<Dialog>639<ForwardMessageComposer stateRef={stateRef} />640<ForwardButton onPress={() => submit(stateRef.current)} />641</Dialog>642)643}644```645646**Correct: state lifted to provider**647648```tsx649function ForwardMessageProvider({ children }: { children: React.ReactNode }) {650const [state, setState] = useState(initialState)651const forwardMessage = useForwardMessage()652const inputRef = useRef(null)653654return (655<Composer.Provider656state={state}657actions={{ update: setState, submit: forwardMessage }}658meta={{ inputRef }}659>660{children}661</Composer.Provider>662)663}664665function ForwardMessageDialog() {666return (667<ForwardMessageProvider>668<Dialog>669<ForwardMessageComposer />670<MessagePreview /> {/* Custom components can access state and actions */}671<DialogActions>672<CancelButton />673<ForwardButton /> {/* Custom components can access state and actions */}674</DialogActions>675</Dialog>676</ForwardMessageProvider>677)678}679680function ForwardButton() {681const { actions } = use(Composer.Context)682return <Button onPress={actions.submit}>Forward</Button>683}684```685686The ForwardButton lives outside the Composer.Frame but still has access to the687688submit action because it's within the provider. Even though it's a one-off689690component, it can still access the composer's state and actions from outside the691692UI itself.693694**Key insight:** Components that need shared state don't have to be visually695696nested inside each other—they just need to be within the same provider.697698---699700## 3. Implementation Patterns701702**Impact: MEDIUM**703704Specific techniques for implementing compound components and705context providers.706707### 3.1 Create Explicit Component Variants708709**Impact: MEDIUM (self-documenting code, no hidden conditionals)**710711Instead of one component with many boolean props, create explicit variant712713components. Each variant composes the pieces it needs. The code documents714715itself.716717**Incorrect: one component, many modes**718719```tsx720// What does this component actually render?721<Composer722isThread723isEditing={false}724channelId='abc'725showAttachments726showFormatting={false}727/>728```729730**Correct: explicit variants**731732```tsx733// Immediately clear what this renders734<ThreadComposer channelId="abc" />735736// Or737<EditMessageComposer messageId="xyz" />738739// Or740<ForwardMessageComposer messageId="123" />741```742743Each implementation is unique, explicit and self-contained. Yet they can each744745use shared parts.746747**Implementation:**748749```tsx750function ThreadComposer({ channelId }: { channelId: string }) {751return (752<ThreadProvider channelId={channelId}>753<Composer.Frame>754<Composer.Input />755<AlsoSendToChannelField channelId={channelId} />756<Composer.Footer>757<Composer.Formatting />758<Composer.Emojis />759<Composer.Submit />760</Composer.Footer>761</Composer.Frame>762</ThreadProvider>763)764}765766function EditMessageComposer({ messageId }: { messageId: string }) {767return (768<EditMessageProvider messageId={messageId}>769<Composer.Frame>770<Composer.Input />771<Composer.Footer>772<Composer.Formatting />773<Composer.Emojis />774<Composer.CancelEdit />775<Composer.SaveEdit />776</Composer.Footer>777</Composer.Frame>778</EditMessageProvider>779)780}781782function ForwardMessageComposer({ messageId }: { messageId: string }) {783return (784<ForwardMessageProvider messageId={messageId}>785<Composer.Frame>786<Composer.Input placeholder="Add a message, if you'd like." />787<Composer.Footer>788<Composer.Formatting />789<Composer.Emojis />790<Composer.Mentions />791</Composer.Footer>792</Composer.Frame>793</ForwardMessageProvider>794)795}796```797798Each variant is explicit about:799800- What provider/state it uses801802- What UI elements it includes803804- What actions are available805806No boolean prop combinations to reason about. No impossible states.807808### 3.2 Prefer Composing Children Over Render Props809810**Impact: MEDIUM (cleaner composition, better readability)**811812Use `children` for composition instead of `renderX` props. Children are more813814readable, compose naturally, and don't require understanding callback815816signatures.817818**Incorrect: render props**819820```tsx821function Composer({822renderHeader,823renderFooter,824renderActions,825}: {826renderHeader?: () => React.ReactNode827renderFooter?: () => React.ReactNode828renderActions?: () => React.ReactNode829}) {830return (831<form>832{renderHeader?.()}833<Input />834{renderFooter ? renderFooter() : <DefaultFooter />}835{renderActions?.()}836</form>837)838}839840// Usage is awkward and inflexible841return (842<Composer843renderHeader={() => <CustomHeader />}844renderFooter={() => (845<>846<Formatting />847<Emojis />848</>849)}850renderActions={() => <SubmitButton />}851/>852)853```854855**Correct: compound components with children**856857```tsx858function ComposerFrame({ children }: { children: React.ReactNode }) {859return <form>{children}</form>860}861862function ComposerFooter({ children }: { children: React.ReactNode }) {863return <footer className='flex'>{children}</footer>864}865866// Usage is flexible867return (868<Composer.Frame>869<CustomHeader />870<Composer.Input />871<Composer.Footer>872<Composer.Formatting />873<Composer.Emojis />874<SubmitButton />875</Composer.Footer>876</Composer.Frame>877)878```879880**When render props are appropriate:**881882```tsx883// Render props work well when you need to pass data back884<List885data={items}886renderItem={({ item, index }) => <Item item={item} index={index} />}887/>888```889890Use render props when the parent needs to provide data or state to the child.891892Use children when composing static structure.893894---895896## 4. React 19 APIs897898**Impact: MEDIUM**899900React 19+ only. Don't use `forwardRef`; use `use()` instead of `useContext()`.901902### 4.1 React 19 API Changes903904**Impact: MEDIUM (cleaner component definitions and context usage)**905906> **⚠️ React 19+ only.** Skip this if you're on React 18 or earlier.907908In React 19, `ref` is now a regular prop (no `forwardRef` wrapper needed), and `use()` replaces `useContext()`.909910**Incorrect: forwardRef in React 19**911912```tsx913const ComposerInput = forwardRef<TextInput, Props>((props, ref) => {914return <TextInput ref={ref} {...props} />915})916```917918**Correct: ref as a regular prop**919920```tsx921function ComposerInput({ ref, ...props }: Props & { ref?: React.Ref<TextInput> }) {922return <TextInput ref={ref} {...props} />923}924```925926**Incorrect: useContext in React 19**927928```tsx929const value = useContext(MyContext)930```931932**Correct: use instead of useContext**933934```tsx935const value = use(MyContext)936```937938`use()` can also be called conditionally, unlike `useContext()`.939940---941942## References9439441. [https://react.dev](https://react.dev)9452. [https://react.dev/learn/passing-data-deeply-with-context](https://react.dev/learn/passing-data-deeply-with-context)9463. [https://react.dev/reference/react/use](https://react.dev/reference/react/use)947