id, title, category, status, source_trust_level, verification_status, created_at, updated_at, tags, tech_stack, applied_in, aliases
| id |
title |
category |
status |
source_trust_level |
verification_status |
created_at |
updated_at |
tags |
tech_stack |
applied_in |
aliases |
| react-component-composition |
컴포넌트 합성 (Composition over Configuration) |
Coding |
draft |
B |
conceptual |
2026-05-09 |
2026-05-09 |
| react |
| composition |
| children |
| slots |
| vibe-coding |
|
| language |
applicable_to |
| TypeScript / React 18+ |
|
|
|
| render props |
| compound components |
| slot pattern |
|
컴포넌트 합성 (Composition)
새 옵션 props 추가가 답답해지면 멈춰라. 자식이 직접 채우게 하는 합성이 prop 폭발보다 거의 항상 낫다. boolean prop 30개 컴포넌트는 anti-signal.
📖 핵심 개념
3가지 합성 패턴:
- Children pass-through:
{children} 받아 가운데에 끼움
- Slot props (named children):
header / footer 등 이름 있는 영역
- Compound components:
<Tabs><Tab/><Tab/></Tabs> 같이 부모 + 자식 협력
💻 코드 패턴
Children pass-through
Slot props
Compound components
🤔 의사결정 기준
| 상황 |
패턴 |
| 단순 wrapper (border, padding) |
children |
| 정해진 레이아웃, 영역 의미 다름 |
slot props |
| 부모-자식 상태 공유 (Tabs, Accordion, Menu) |
compound components |
| 외부에서 마음대로 조립 가능해야 |
render props 또는 hook 노출 |
| 옵션이 5개 이상의 boolean prop |
composition 으로 리팩터 |
❌ 안티패턴
- boolean prop 폭발:
<Button primary danger small loading rounded outlined ... />. variant prop 도입 또는 합성.
- 자식 종류 검사 후 강제 (
children.type === Tab): 깨지기 쉬움. Context 기반 통신.
- render props 남발: hook 으로 충분한데 함수 prop 일렬. hook 권장.
- slot 인데 ReactNode 가 아닌 string 받기: 유연성 손실. 보통 ReactNode.
- Compound 인데 Context 없이 구현: prop drilling 또는 imperative 검사.
🤖 LLM 활용 힌트
- "이 컴포넌트의 prop 가 5개 이상 boolean 이면 합성으로" 강조.
- compound 패턴은 ARIA 속성도 같이 챙겨야 — accessibility 검토 명시.
🔗 관련 문서