Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | 7 |
8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 27 | 28 |
29 | 30 |
Tags
- Supabase
- 타입스크립트
- 동적계획법
- 리액트 패턴
- react router dom
- 그리디
- 프론트엔드
- Form
- reduxtoolkit
- 리액트
- 프로그래머스
- Next.js
- revalidatepath
- tanstack query
- 리액트 라우터 돔
- JavaScript
- 코어자바스크립트
- 토이 프로젝트
- styled component
- tailwind
- 코딩테스트
- 리덕스
- 스택
- 토이프로젝트
- 자바스크립트
- 코테
- TypeScript
- React
- react pattern
- React Query
Archives
- Today
- Total
느려도 한걸음씩
Props를 좀 더 유연하게 다루는 방법 본문
리액트에서 props를 좀더 유연하게 다뤄보자
import { EXAMPLES } from "../data";
import TabButton from "./TabButton";
import { useState } from "react";
import Section from "./Section";
import Tabs from "./Tabs";
export default function Examples() {
const [selectedTopic, setSelectedTopic] = useState();
let tabContent = <p>Please select a topic.</p>;
if (selectedTopic) {
tabContent = (
<div id='tab-content'>
<h3>{EXAMPLES[selectedTopic].title}</h3>
<p>{EXAMPLES[selectedTopic].description}</p>
<pre>
<code>{EXAMPLES[selectedTopic].code}</code>
</pre>
</div>
);
}
function handleSelect(selectedButton) {
setSelectedTopic(selectedButton);
}
return (
<Section title='Examples' id='examples'>
<Tabs
buttons={
<>
<TabButton
isSelected={selectedTopic === "Components"}
onClick={() => handleSelect("Components")}
>
Components
</TabButton>
<TabButton
isSelected={selectedTopic === "JSX"}
onClick={() => handleSelect("JSX")}
>
JSX
</TabButton>
<TabButton
isSelected={selectedTopic === "Props"}
onClick={() => handleSelect("Props")}
>
Props
</TabButton>
<TabButton
isSelected={selectedTopic === "State"}
onClick={() => handleSelect("State")}
>
State
</TabButton>
</>
}
>
{tabContent}
</Tabs>
</Section>
);
}
export default function Tabs({ children, buttons, ButtonsContainer = "menu" }) {
return (
<>
<ButtonsContainer>{buttons}</ButtonsContainer>
{children}
</>
);
}
현재 Tabs 컴포넌트에 children, buttons, ButtonsContainer 3가지 props를 전달하고 있다
children은 커스텀 컴포넌트의 태그 사이에 들어갈 내용을 정의할수 있는 props이다
buttons는 JSX 요소를 자바스크립트 표현식을 통해서 props를 보내고 있다 이렇게 적어주면 JSX 요소를 그대로 props로
보내 해당 컴포넌트의 원하는 자리에 위치시킬수 있다
마지막으로 ButtonsContainer의 경우 menu라는 string값을 default값으로 받고있다 이렇게 작성해주면 ButtonsContainer라는 이름으로 menu라는 내장태그가 적용된다 만약 ButtonsContainer라는 이름으로 div태그를 만들고 싶으면
export default function Tabs({ children, buttons, ButtonsContainer = "div" }) {
return (
<>
<ButtonsContainer>{buttons}</ButtonsContainer>
{children}
</>
);
}
이렇게 작성해주면 된다 그리고 중요한 점은 태그 이름을 props로 보낼때는 맨 앞글자를 대문자로 작성해 리액트가 컴포넌트라고 인식하게 만들어줘야한다
만약 내장태그아 아닌 본인이 정의한 다른 컴포넌트를 태그로 하고 싶다면 JSX를 props로 보내준것처럼 표현식을 이용해야한다
ButtonsContainer={Section}
import { EXAMPLES } from "../data";
import TabButton from "./TabButton";
import { useState } from "react";
import Section from "./Section";
import Tabs from "./Tabs";
export default function Examples() {
const [selectedTopic, setSelectedTopic] = useState();
let tabContent = <p>Please select a topic.</p>;
if (selectedTopic) {
tabContent = (
<div id='tab-content'>
<h3>{EXAMPLES[selectedTopic].title}</h3>
<p>{EXAMPLES[selectedTopic].description}</p>
<pre>
<code>{EXAMPLES[selectedTopic].code}</code>
</pre>
</div>
);
}
function handleSelect(selectedButton) {
setSelectedTopic(selectedButton);
}
return (
<Section title='Examples' id='examples'>
<Tabs
ButtonsContainer={Section}
buttons={
<>
<TabButton
isSelected={selectedTopic === "Components"}
onClick={() => handleSelect("Components")}
>
Components
</TabButton>
<TabButton
isSelected={selectedTopic === "JSX"}
onClick={() => handleSelect("JSX")}
>
JSX
</TabButton>
<TabButton
isSelected={selectedTopic === "Props"}
onClick={() => handleSelect("Props")}
>
Props
</TabButton>
<TabButton
isSelected={selectedTopic === "State"}
onClick={() => handleSelect("State")}
>
State
</TabButton>
</>
}
>
{tabContent}
</Tabs>
</Section>
);
}
'FE develop > React' 카테고리의 다른 글
React 컴포넌트에서 DOM으로 변환되는 과정 (1) | 2024.11.15 |
---|---|
React - 컴포넌트의 세가지 카테고리 (0) | 2024.11.14 |
React에서 컴포넌트 속성을 효율적으로 전달하는 방법 (0) | 2024.03.28 |
React에서 이벤트 핸들러 전달하기: 함수 자체 vs 화살표 함수 (0) | 2024.03.27 |
Props를 전달하는 여러가지 방법 (0) | 2024.03.27 |