97 lines
3.7 KiB
Markdown
97 lines
3.7 KiB
Markdown
---
|
|
id: react-component-composition
|
|
title: 컴포넌트 합성 (Composition over Configuration)
|
|
category: Coding
|
|
status: draft
|
|
source_trust_level: B
|
|
verification_status: conceptual
|
|
created_at: 2026-05-09
|
|
updated_at: 2026-05-09
|
|
tags: [react, composition, children, slots, vibe-coding]
|
|
tech_stack: { language: "TypeScript / React 18+", applicable_to: ["Web", "React Native"] }
|
|
applied_in: []
|
|
aliases: [render props, compound components, slot pattern]
|
|
---
|
|
|
|
# 컴포넌트 합성 (Composition)
|
|
|
|
> 새 옵션 props 추가가 답답해지면 멈춰라. **자식이 직접 채우게 하는 합성**이 prop 폭발보다 거의 항상 낫다. boolean prop 30개 컴포넌트는 anti-signal.
|
|
|
|
## 📖 핵심 개념
|
|
3가지 합성 패턴:
|
|
1. **Children pass-through**: `{children}` 받아 가운데에 끼움
|
|
2. **Slot props (named children)**: `header` / `footer` 등 이름 있는 영역
|
|
3. **Compound components**: `<Tabs><Tab/><Tab/></Tabs>` 같이 부모 + 자식 협력
|
|
|
|
## 💻 코드 패턴
|
|
|
|
### Children pass-through
|
|
```tsx
|
|
function Card({ children }: { children: ReactNode }) {
|
|
return <div className="card-shell">{children}</div>;
|
|
}
|
|
<Card><h2>Title</h2><p>body</p></Card>
|
|
```
|
|
|
|
### Slot props
|
|
```tsx
|
|
function Modal({ title, body, actions }: { title: ReactNode; body: ReactNode; actions: ReactNode }) {
|
|
return <div className="modal">
|
|
<header>{title}</header>
|
|
<section>{body}</section>
|
|
<footer>{actions}</footer>
|
|
</div>;
|
|
}
|
|
<Modal title="삭제" body="정말?" actions={<><button>취소</button><button>삭제</button></>} />
|
|
```
|
|
|
|
### Compound components
|
|
```tsx
|
|
const TabsContext = createContext<{ active: string; setActive: (k: string) => void } | null>(null);
|
|
|
|
function Tabs({ defaultActive, children }: ...) {
|
|
const [active, setActive] = useState(defaultActive);
|
|
return <TabsContext.Provider value={{ active, setActive }}>{children}</TabsContext.Provider>;
|
|
}
|
|
function TabList({ children }) { return <div role="tablist">{children}</div>; }
|
|
function Tab({ id, children }: { id: string; children: ReactNode }) {
|
|
const ctx = useContext(TabsContext)!;
|
|
return <button onClick={() => ctx.setActive(id)} aria-selected={ctx.active === id}>{children}</button>;
|
|
}
|
|
function TabPanel({ id, children }) {
|
|
const ctx = useContext(TabsContext)!;
|
|
return ctx.active === id ? <div>{children}</div> : null;
|
|
}
|
|
|
|
Tabs.List = TabList; Tabs.Tab = Tab; Tabs.Panel = TabPanel;
|
|
|
|
<Tabs defaultActive="a">
|
|
<Tabs.List><Tabs.Tab id="a">A</Tabs.Tab><Tabs.Tab id="b">B</Tabs.Tab></Tabs.List>
|
|
<Tabs.Panel id="a">…</Tabs.Panel><Tabs.Panel id="b">…</Tabs.Panel>
|
|
</Tabs>
|
|
```
|
|
|
|
## 🤔 의사결정 기준
|
|
| 상황 | 패턴 |
|
|
|---|---|
|
|
| 단순 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 검토 명시.
|
|
|
|
## 🔗 관련 문서
|
|
- [[React_Custom_Hook_Patterns]]
|
|
- [[React_Refs_Patterns]]
|