Compound Components๋? ๐ก
**Compound Components(์ปดํ์ด๋ ์ปดํฌ๋ํธ)**๋ React์์ ์ ์ฐํ๊ณ ์ฌ์ฌ์ฉ ๊ฐ๋ฅํ UI ์ปดํฌ๋ํธ๋ฅผ ์ค๊ณํ๊ธฐ ์ํ ํจํด์ด์์. ์ด ํจํด์ ํ์ฉํ๋ฉด ์ปดํฌ๋ํธ ๋ด๋ถ์ ๊ด๊ณ๋ฅผ ๋ช ์์ ์ผ๋ก ๋ํ๋ด๊ณ , ์ฌ์ฉ์๊ฐ ๋ ์ง๊ด์ ์ผ๋ก ์ปดํฌ๋ํธ๋ฅผ ๊ตฌ์ฑํ ์ ์๋๋ก ๋์์ค๋๋ค.
์ฝ๊ฒ ๋งํด, ์ปดํฌ๋ํธ๋ฅผ “์์-ํ์ ๊ด๊ณ”๋ก ๋ฌถ์ด์ ๋์ํ๋๋ก ์ค๊ณํ๋ ๊ฑฐ์์. ๊ทธ๋ผ Compound Components์ ์ ์์ ์ฅ์ , ๊ทธ๋ฆฌ๊ณ ์ด๋ป๊ฒ ์ฌ์ฉํ๋์ง ์์๋ณผ๊น์? ๐
Compound Components์ ์ ์
Compound Components๋ ์ปจํ ์ด๋ ์ปดํฌ๋ํธ์ ์ฌ๋ฌ ํ์ ์ปดํฌ๋ํธ๊ฐ ํจ๊ป ๋์ํ๋๋ก ์ค๊ณ๋ ํจํด์ด์์.
์ปจํ ์ด๋ ์ปดํฌ๋ํธ๊ฐ ์ํ๋ฅผ ๊ด๋ฆฌํ๊ณ , ํ์ ์ปดํฌ๋ํธ๋ ์ด๋ฅผ ์ฐธ์กฐํด ํ์ํ UI์ ๋์์ ์ ์ํฉ๋๋ค.
๐ ๏ธ ์ฌ์ฉ ์์: Tab ์ปดํฌ๋ํธ
import React, { useState, createContext, useContext } from "react";
const TabContext = createContext();
const Tabs = ({ children }) => {
const [activeTab, setActiveTab] = useState(0);
return (
<TabContext.Provider value={{ activeTab, setActiveTab }}>
<div className="tabs">{children}</div>
</TabContext.Provider>
);
};
const TabList = ({ children }) => <div className="tab-list">{children}</div>;
const Tab = ({ children, index }) => {
const { activeTab, setActiveTab } = useContext(TabContext);
return (
<button
className={`tab ${activeTab === index ? "active" : ""}`}
onClick={() => setActiveTab(index)}
>
{children}
</button>
);
};
const TabPanels = ({ children }) => {
const { activeTab } = useContext(TabContext);
return <div className="tab-panels">{children[activeTab]}</div>;
};
const TabPanel = ({ children }) => <div className="tab-panel">{children}</div>;
// ์ฌ์ฉ ์์
export default function App() {
return (
<Tabs>
<TabList>
<Tab index={0}>Tab 1</Tab>
<Tab index={1}>Tab 2</Tab>
<Tab index={2}>Tab 3</Tab>
</TabList>
<TabPanels>
<TabPanel>Content 1</TabPanel>
<TabPanel>Content 2</TabPanel>
<TabPanel>Content 3</TabPanel>
</TabPanels>
</Tabs>
);
}
๐ ์ฝ๋ ๋ถ์
1. Tabs: ์์ ์ปดํฌ๋ํธ๋ก ์ํ(activeTab)๋ฅผ ๊ด๋ฆฌํ๊ณ ํ์ ์ปดํฌ๋ํธ๋ค์๊ฒ ์ ๋ฌํฉ๋๋ค.
2. TabList: ํญ ๋ฒํผ๋ค์ ๊ทธ๋ฃนํํ๋ ์ญํ ์ ํฉ๋๋ค.
3. Tab: ๊ฐ๋ณ ํญ ๋ฒํผ์ผ๋ก, ํด๋ฆญํ๋ฉด activeTab ์ํ๋ฅผ ๋ณ๊ฒฝํฉ๋๋ค.
4. TabPanels: ํ์ฌ ํ์ฑํ๋ ํญ์ ์ฝํ ์ธ ๋ฅผ ๋ ๋๋งํฉ๋๋ค.
5. TabPanel: ๊ฐ ํญ์ ์ฝํ ์ธ ๋ฅผ ์ ์ํฉ๋๋ค.
์ Compound Components๋ฅผ ์ฌ์ฉํ ๊น?
1๏ธโฃ UI ๊ตฌ์ฑ ์์ ๊ฐ์ ๊ด๊ณ๋ฅผ ๋ช ํํ ํํ
Tabs, TabList, TabPanels ๊ฐ์ ํ์ ์ปดํฌ๋ํธ๋ค์ด ์์ ์ปดํฌ๋ํธ์ ๋ฐ์ ํ๊ฒ ์ฐ๊ฒฐ๋ ๊ด๊ณ์์ ์ฝ๋ ๊ตฌ์กฐ์์ผ๋ก ๋ช ํํ ๋๋ฌ๋ผ ์ ์์ด์.
2๏ธโฃ ์ฌ์ฉ์ ๊ฒฝํ ๊ฐ์
์ปดํฌ๋ํธ๋ฅผ ์ ์ธ์ ์ผ๋ก ์์ฑํ๊ฒ ๋ง๋ค์ด ์ฌ์ฉ์(๋ค๋ฅธ ๊ฐ๋ฐ์) ์ ์ฅ์์ ์ง๊ด์ ์ด๊ณ ์ดํดํ๊ธฐ ์ฝ๊ฒ ๋ง๋ญ๋๋ค.
3๏ธโฃ ์ ์ฐ์ฑ ์ฆ๊ฐ
์ฌ์ฉ์๋ ์ปดํฌ๋ํธ๋ฅผ ์์ ๋กญ๊ฒ ๊ตฌ์ฑํ๊ฑฐ๋ ์์๋ฅผ ์กฐ์ ํ ์ ์์ด ๋ ๋ค์ํ UI๋ฅผ ๊ตฌํํ ์ ์์ด์.
Compound Components๋ฅผ ์ค๊ณํ ๋์ ํต์ฌ ํฌ์ธํธ
1๏ธโฃ Context API ํ์ฉ
์์ ์ปดํฌ๋ํธ์์ ์ํ๋ฅผ ๊ด๋ฆฌํ๊ณ , Context๋ฅผ ํตํด ํ์ ์ปดํฌ๋ํธ์ ์ ๋ฌํฉ๋๋ค. ์ด๋ฅผ ํตํด ์ปดํฌ๋ํธ ๊ฐ ๋ฐ์ดํฐ ๊ณต์ ๋ฅผ ์ฝ๊ฒ ํ ์ ์์ด์.
2๏ธโฃ Props.children ์ฌ์ฉ
React์ props.children์ ํ์ฉํ๋ฉด ์ฌ์ฉ์๊ฐ ์ํ๋ ๋๋ก ํ์ ์ปดํฌ๋ํธ๋ฅผ ๊ตฌ์ฑํ ์ ์์ด์.
3๏ธโฃ ๊ตฌ์ฑ ์์ ๊ฐ ๊ฒฐํฉ๋ ๋ฎ์ถ๊ธฐ
๊ฐ ํ์ ์ปดํฌ๋ํธ๋ฅผ ๋ ๋ฆฝ์ ์ผ๋ก ํ ์คํธ ๊ฐ๋ฅํ๋๋ก ์ค๊ณํ์ธ์. ์ด๋ฅผ ํตํด ์ ์ง๋ณด์์ฑ์ด ๋์์ง๋๋ค.
Compound Components์ ํ์ฉ ์ฌ๋ก
๐ 1. Accordion
<Accordion>
<AccordionItem>
<AccordionHeader>Header 1</AccordionHeader>
<AccordionPanel>Content 1</AccordionPanel>
</AccordionItem>
<AccordionItem>
<AccordionHeader>Header 2</AccordionHeader>
<AccordionPanel>Content 2</AccordionPanel>
</AccordionItem>
</Accordion>
๐ 2. Form
<Form>
<FormField label="Username">
<Input type="text" />
</FormField>
<FormField label="Password">
<Input type="password" />
</FormField>
</Form>
Compound Components๋ฅผ ์ฌ์ฉํ ๋ ์ฃผ์ํ ์
1. ๋ณต์ก๋๋ฅผ ์กฐ์ ํ์ธ์
๋๋ฌด ๋ง์ ํ์ ์ปดํฌ๋ํธ๋ฅผ ๋ง๋ค๋ฉด ๊ด๋ฆฌ๊ฐ ์ด๋ ค์์ง ์ ์์ด์. ์ ์ ํ ์ถ์ํ๋ฅผ ์ ์งํ๋ ๊ฒ์ด ์ค์ํฉ๋๋ค.
2. Context ์ฌ์ฉ ์ ์ต์ ํ ํ์
Context ๊ฐ์ด ์์ฃผ ๋ณ๊ฒฝ๋๋ฉด ๋ชจ๋ ํ์ ์ปดํฌ๋ํธ๊ฐ ๋ฆฌ๋ ๋๋ง๋ ์ ์์ผ๋ React.memo์ ๊ฐ์ ์ต์ ํ ๊ธฐ๋ฒ์ ํ์ฉํ์ธ์.
3. ๋ช ํํ ๋ฌธ์ํ
Compound Components ํจํด์ ์ค๊ณ๊ฐ ๋ณต์กํด์ง ๊ฐ๋ฅ์ฑ์ด ์์ผ๋ฏ๋ก, ์ฌ์ฉ์(๊ฐ๋ฐ์)๋ฅผ ์ํ ๋ช ํํ ๋ฌธ์ํ๊ฐ ํ์์ ๋๋ค.
๊ฒฐ๋ก
Compound Components๋ UI ๊ตฌ์ฑ ์์ ๊ฐ์ ๊ด๊ณ๋ฅผ ๋ช ํํ ํ๊ณ , ์ฌ์ฌ์ฉ ๊ฐ๋ฅํ ์ปดํฌ๋ํธ๋ฅผ ์ค๊ณํ ์ ์๋ ๊ฐ๋ ฅํ ํจํด์ด์์. Context API, props.children, ๊ทธ๋ฆฌ๊ณ React์ ์ปดํฌ๋ํธ ๊ธฐ๋ฐ ์ค๊ณ ์ฒ ํ์ ์ ํ์ฉํ๋ฉด ๋์ฑ ์ ์ฐํ๊ณ ์ ์ง๋ณด์ํ๊ธฐ ์ฌ์ด ์ฝ๋๋ฅผ ์์ฑํ ์ ์์ต๋๋ค.
๐ท์ ์ค์ ๊ฐ๋ฐ์๊ฐ ๋์ด๋ด ์๋น!๐ท
'Front-end > Good Front' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
๐จ๏ธ Giscus: GitHub ๊ธฐ๋ฐ์ ๋๊ธ ์์คํ (0) | 2025.03.06 |
---|---|
๐ gray-matter: Markdown ํ์ผ์ ๋ฉํ๋ฐ์ดํฐ๋ฅผ ๋ค๋ฃจ๋ ๋ง๋ฒ ๋๊ตฌ (0) | 2025.03.05 |
๐ก ํ๋ก ํธ์๋์์ IoC๋ ๋ฌด์์ธ๊ฐ์? (0) | 2025.02.21 |