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 | 31 |
Tags
- Git
- review
- PyTorch
- Express
- vscode
- nodejs
- Linux
- frontend
- DB
- postgresql
- python
- CV
- js
- SOLID
- CSS
- C++
- PRISMA
- DM
- GAN
- react
- ps
- figma
- ML
- UI
- sqlite
- threejs
- API
- mongo
- html
- ts
Archives
- Today
- Total
아카이브
[React] Syntax | 02. children prop 본문
React에는 기본적으로 제공되는 prop이 있는데, 그것이 바로 children prop입니다.
children은 컴포넌트의 여는 태그<>와 닫는 태그</> 사이에 입력된 값을 받는 prop입니다.
아래 코드는 'but A', 'but B'라는 이름의 버튼 두 개를 출력합니다.
이런 경우처럼 단순히 보여지기만 하는 값을 다룰 때는 좀 더 직관적으로 코드를 작성할 수 있습니다.
// *** Button.js *** //
function Button({ text }) {
return <button>{text}</button>;
}
export default Button;
// *** App.js *** //
import Button from './Button';
function App(){
return(
<div>
<Button text="but A" />
<Button text="but B" />
</div>
);
}
export default App;
아래 코드는 Button 컴포넌트에서 text 대신 childern prop을 활용한 경우입니다.
이 경우, Button.js 파일을 보지 않더라도 Button 컴포넌트 태그 사이의 값이 버튼의 텍스트로 들어간다는 것을 쉽게 이해할 수 있습니다.
// *** Button.js *** //
function Button({ children }) {
return <button>{children}</button>;
}
export default Button;
// *** App.js *** //
import Button from './Button';
function App(){
return(
<div>
<Button>but A</Button>
<Button>but B</Button>
</div>
);
}
export default App;
물론, 이 children prop도 다른 prop들과 함께 사용할 수 있습니다.
function Button({ children, onClick }) {
return <button onClick={onClick}>{children}</button>;
}
export default Button;
728x90
'Frontend > React' 카테고리의 다른 글
[React] React 프로젝트 빌드하기 (0) | 2024.06.17 |
---|---|
[React] Syntax | 03. State와 반응성 (0) | 2024.06.10 |
[React] Syntax | 01. Component (0) | 2024.06.10 |
[React] Chrome에서 React 개발자 도구 설치하기 (0) | 2024.05.19 |
[React] React 프로젝트 시작하기 (0) | 2024.05.19 |
Comments