Files
2nd/10_Wiki/Topic_Programming/Coding/React_Component_Composition.md
T
Antigravity Agent 9148c358d0 docs(10_Wiki): 위키 전체 재구성 — Topic_* 폴더를 4개 카테고리로 통합 + 대규모 중복 제거
Topic_Agent/Topic_Blog/Topics/Topics_Biz/Topics_Meeting/Topics_Rag의 마크다운 지식 문서를
Topic_General/Topic_Programming/Topic_Graphic/Topic_Business 4개 카테고리로 재분류.

- 중복 제거: frontmatter의 status:duplicate/merged + duplicate_of/redirect_to 필드로
  자기 자신을 중복으로 선언한 리다이렉트 stub 1032개 제거, 완전 동일 내용 파일 472개 제거,
  동일 파일명·다른 내용 충돌 시 더 큰(완전한) 버전만 유지(162개 제거) — 총 1639개 중복 제거.
- 분류: 폴더 단위로 명확한 항목(AI_and_ML/Coding/Architecture 등 → Programming,
  Comfyui/Visual_Effects → Graphic, Topics_Biz/Topics_Meeting/사업 등 → Business,
  Poetic_Blog_Writing/창의성/Game_Design 등 → General)은 폴더 우선순위로,
  나머지 혼재 폴더(Topic_Agent/Topic_Blog/Topics 루트/Thinking & Reasoning/Other/UI_UX_Assets)는
  title/tags 키워드 스코어링으로 파일 단위 분류(불명확한 경우 General로 폴백).
  원본 폴더명은 "From_*" 서브폴더로 보존해 추적 가능성 유지.
- 최종 배치: Programming 2784 / General 1608 / Graphic 285 / Business 249 = 4926개 문서.
- 에이전트 운영 상태(.astra/.agent/.obsidian/sessions/memory/_company/docs/lessons/_shared/src)는
  지식 콘텐츠가 아니므로 재분류 대상에서 제외하고 원위치 유지.
- Topics/Topic_email(상위 보호 폴더 Topic_email과 파일명 100% 중복) 삭제 — 보호 폴더 자체는 미변경.
- 완전히 비게 된 Topic_Agent/Topic_Blog/Topics_Biz/Topics_Rag 폴더 제거.
2026-07-05 00:33:48 +09:00

3.7 KiB

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+
Web
React Native
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

function Card({ children }: { children: ReactNode }) {
  return <div className="card-shell">{children}</div>;
}
<Card><h2>Title</h2><p>body</p></Card>

Slot props

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

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 검토 명시.

🔗 관련 문서