Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Build performant React Native and Expo apps with best practices for lists, animations, and navigation
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
rules/design-system-compound-components.md
1---2title: Use Compound Components Over Polymorphic Children3impact: MEDIUM4impactDescription: flexible composition, clearer API5tags: design-system, components, composition6---78## Use Compound Components Over Polymorphic Children910Don't create components that can accept a string if they aren't a text node. If11a component can receive a string child, it must be a dedicated `*Text`12component. For components like buttons, which can have both a View (or13Pressable) together with text, use compound components, such a `Button`,14`ButtonText`, and `ButtonIcon`.1516**Incorrect (polymorphic children):**1718```tsx19import { Pressable, Text } from 'react-native'2021type ButtonProps = {22children: string | React.ReactNode23icon?: React.ReactNode24}2526function Button({ children, icon }: ButtonProps) {27return (28<Pressable>29{icon}30{typeof children === 'string' ? <Text>{children}</Text> : children}31</Pressable>32)33}3435// Usage is ambiguous36<Button icon={<Icon />}>Save</Button>37<Button><CustomText>Save</CustomText></Button>38```3940**Correct (compound components):**4142```tsx43import { Pressable, Text } from 'react-native'4445function Button({ children }: { children: React.ReactNode }) {46return <Pressable>{children}</Pressable>47}4849function ButtonText({ children }: { children: React.ReactNode }) {50return <Text>{children}</Text>51}5253function ButtonIcon({ children }: { children: React.ReactNode }) {54return <>{children}</>55}5657// Usage is explicit and composable58<Button>59<ButtonIcon><SaveIcon /></ButtonIcon>60<ButtonText>Save</ButtonText>61</Button>6263<Button>64<ButtonText>Cancel</ButtonText>65</Button>66```67