[React] CustomHook ๐ useTabs
https://nomadcoders.co ์์ ๊ฐ์๋ฅผ ๋ฃ๊ณ ์ ๋ฆฌํ ๋ด์ฉ ์ ๋๋ค.
1. useTabs ์ด๋
- button tab ์ด๋ผ๊ณ ์๊ฐํ๋ฉด ๋๋ค!
2. useTabs ์์ฑํ๊ธฐ
const useTabs = (initialTab, allTabs) => {
const [currentIndex, setCurrentIndex] = useState(initialTab);
// function ์์ํ ๋ ์๋ฌ ํ์ธ
if (!allTabs || !Array.isArray(allTabs)) {
return;
}
// index
return {
currentItem: allTabs[currentIndex],
changeItem: setCurrentIndex
}
}
3. useTabs ์ฌ์ฉํ๊ธฐ
- ๋จผ์ ๊ฐ์ ธ์ฌ ๋ฐ์ดํฐ๋ฅผ ๋ง๋ค์ด์ค๋ค.
- api๋ฅผ ๊ฐ์ ธ์ค๋ ๋ฑ์ผ๋ก ํ์ฉํ ์ ์๋ค!
const content = [
{
tab: "section 1",
content: "section1์ content"
},
{
tab: "section 2",
content: "section2์ content"
},
{
tab: "section 3",
content: "section3์ content"
}
]
- ๋ฐ์ดํฐ ๋ณด์ฌ์ฃผ๊ธฐ
const App = () => {
const {currentItem, changeItem} = useTabs(0, content);
return (
<div className="app">
<div>
{content.map((section, index) =>
<button onClick={() => changeItem(index)}> {section.tab} </button>
)}
</div>
<div> {currentItem.content} </div>
</div>
)
}
4. ์๋ฌ ๋ฐ์..
Each child in a list should have a unique "key" prop.
์ด์ ๊ฐ์ ์๋ฌ๊ฐ ๋ฐ์๊ฐ ๋ฐ์ํ๋ค.
key ๊ฐ์ด ์์ด์ ์๋ฌ๊ฐ ๋ฐ์ํ๋ ๊ฒ...
<button onClick={() => changeItem(index)} key={index}> {section.tab} </button>
key={index}
๋ฅผ ์ฌ์ฉํ์ฌ ํด๊ฒฐํ๋ค!!!
Author And Source
์ด ๋ฌธ์ ์ ๊ดํ์ฌ([React] CustomHook ๐ useTabs), ์ฐ๋ฆฌ๋ ์ด๊ณณ์์ ๋ ๋ง์ ์๋ฃ๋ฅผ ๋ฐ๊ฒฌํ๊ณ ๋งํฌ๋ฅผ ํด๋ฆญํ์ฌ ๋ณด์๋ค https://velog.io/@reemrev21/React-CustomHook-useTab์ ์ ๊ท์: ์์์ ์ ๋ณด๊ฐ ์์์ URL์ ํฌํจ๋์ด ์์ผ๋ฉฐ ์ ์๊ถ์ ์์์ ์์ ์ ๋๋ค.
์ฐ์ํ ๊ฐ๋ฐ์ ์ฝํ ์ธ ๋ฐ๊ฒฌ์ ์ ๋ (Collection and Share based on the CC Protocol.)
์ข์ ์นํ์ด์ง ์ฆ๊ฒจ์ฐพ๊ธฐ
๊ฐ๋ฐ์ ์ฐ์ ์ฌ์ดํธ ์์ง
๊ฐ๋ฐ์๊ฐ ์์์ผ ํ ํ์ ์ฌ์ดํธ 100์ ์ถ์ฒ ์ฐ๋ฆฌ๋ ๋น์ ์ ์ํด 100๊ฐ์ ์์ฃผ ์ฌ์ฉํ๋ ๊ฐ๋ฐ์ ํ์ต ์ฌ์ดํธ๋ฅผ ์ ๋ฆฌํ์ต๋๋ค