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.
rules/patterns-children-over-render-props.md
1---2title: Prefer Composing Children Over Render Props3impact: MEDIUM4impactDescription: cleaner composition, better readability5tags: composition, children, render-props6---78## Prefer Children Over Render Props910Use `children` for composition instead of `renderX` props. Children are more11readable, compose naturally, and don't require understanding callback12signatures.1314**Incorrect (render props):**1516```tsx17function Composer({18renderHeader,19renderFooter,20renderActions,21}: {22renderHeader?: () => React.ReactNode23renderFooter?: () => React.ReactNode24renderActions?: () => React.ReactNode25}) {26return (27<form>28{renderHeader?.()}29<Input />30{renderFooter ? renderFooter() : <DefaultFooter />}31{renderActions?.()}32</form>33)34}3536// Usage is awkward and inflexible37return (38<Composer39renderHeader={() => <CustomHeader />}40renderFooter={() => (41<>42<Formatting />43<Emojis />44</>45)}46renderActions={() => <SubmitButton />}47/>48)49```5051**Correct (compound components with children):**5253```tsx54function ComposerFrame({ children }: { children: React.ReactNode }) {55return <form>{children}</form>56}5758function ComposerFooter({ children }: { children: React.ReactNode }) {59return <footer className='flex'>{children}</footer>60}6162// Usage is flexible63return (64<Composer.Frame>65<CustomHeader />66<Composer.Input />67<Composer.Footer>68<Composer.Formatting />69<Composer.Emojis />70<SubmitButton />71</Composer.Footer>72</Composer.Frame>73)74```7576**When render props are appropriate:**7778```tsx79// Render props work well when you need to pass data back80<List81data={items}82renderItem={({ item, index }) => <Item item={item} index={index} />}83/>84```8586Use render props when the parent needs to provide data or state to the child.87Use children when composing static structure.88