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
- 코어자바스크립트
- 코테
- 리액트
- tanstack query
- JavaScript
- tailwind
- Next.js
- Form
- React Query
- styled component
- 동적계획법
- 토이 프로젝트
- reduxtoolkit
- revalidatepath
- 자바스크립트
- 코딩테스트
- 리덕스
- React
- react router dom
- 리액트 라우터 돔
- 그리디
- 프론트엔드
- react pattern
- 프로그래머스
- TypeScript
- 스택
- 토이프로젝트
- 타입스크립트
- 리액트 패턴
Archives
- Today
- Total
느려도 한걸음씩
Props를 전달하는 여러가지 방법 본문
export const CORE_CONCEPTS = [
{
image: componentsImg,
title: "Components",
description:
"The core UI building block - compose the user interface by combining multiple components.",
},
{
image: jsxImg,
title: "JSX",
description:
"Return (potentially dynamic) HTML(ish) code to define the actual markup that will be rendered.",
},
{
image: propsImg,
title: "Props",
description:
"Make components configurable (and therefore reusable) by passing input data to them.",
},
{
image: stateImg,
title: "State",
description:
"React-managed data which, when changed, causes the component to re-render & the UI to update.",
},
];
이런 데이터가 있다고 쳐보자
<ul>
{CORE_CONCEPTS.map((item, index) => {
return <CoreConcept key={index} />;
})}
</ul>
CoreConcept 컴포넌트로 각각 요소들을 보내 props로 전달해야한다
export default function CoreConcept() {
return (
<li>
<img/>
<h3></h3>
<p></p>
</li>
);
}
1.가장 기본적인 방식
<ul>
{CORE_CONCEPTS.map((item, index) => {
return (
<CoreConcept
title={item.title}
description={item.description}
image={item.image}
key={item.index}
/>
);
})}
</ul>
export default function CoreConcept(props) {
return (
<li>
<img src={props.image} alt={props.title} />
<h3>{props.title}</h3>
<p>{props.description}</p>
</li>
);
}
props객체를 그대로 받아 받아올 프로퍼티를 점 표기법을 이용해 중괄호에 넣어준다
또는 props를 받는 컴포넌트에서 구조 분해할당을 이용해
export default function CoreConcept({ title, description, image }) {
return (
<li>
<img src={image} alt={title} />
<h3>{title}</h3>
<p>{description}</p>
</li>
);
}
이렇게 사용할 수도 있다
2. props를 객체로 그룹화해서 전달
<ul>
{CORE_CONCEPTS.map((item, index) => {
return <CoreConcept {...CORE_CONCEPTS[index]} key={index} />;
})}
</ul>
export default function CoreConcept({ title, description, image }) {
return (
<li>
<img src={image} alt={title} />
<h3>{title}</h3>
<p>{description}</p>
</li>
);
}
props를 사용할때 객체의 값을 다룬다면 반드시 객체에 있는 key의 이름을 그대로 써야한다
또는 이렇게 사용할 수 도 있다
export default function CoreConcept({ ...concept }) {
const { title, description, image } = concept;
return (
<li>
<img src={image} alt={title} />
<h3>{title}</h3>
<p>{description}</p>
</li>
);
}
'FE develop > React' 카테고리의 다른 글
React 컴포넌트에서 DOM으로 변환되는 과정 (1) | 2024.11.15 |
---|---|
React - 컴포넌트의 세가지 카테고리 (0) | 2024.11.14 |
Props를 좀 더 유연하게 다루는 방법 (1) | 2024.03.28 |
React에서 컴포넌트 속성을 효율적으로 전달하는 방법 (0) | 2024.03.28 |
React에서 이벤트 핸들러 전달하기: 함수 자체 vs 화살표 함수 (0) | 2024.03.27 |