| ์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
|---|---|---|---|---|---|---|
| 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 |
- ํด๋์ค์ ๋ ํฐ
- fontweight
- liststyletype
- textdecoration
- ํฐํธ์จ์ดํธ
- borderstyle
- ์์ด๋์ ๋ ํฐ
- ์ถ์ฝ
- ํ์ ์ ๋ ํฐ
- ๊ทธ๋ฃน์ ๋ ํฐ
- ์ฃผ์
- textshadow
- ํผํ๊ทธ
- ๊ตฌ๊ธํฐํธ
- bordercollapse
- ๋นํ๊ทธ
- wordspacing
- ์ ๋๋ฒ์ ์ ๋ ํฐ
- gtammertable
- captionside
- selectorslevel4
- ํฅ์ค์ฝ๋
- inlineblock
- liststyleposition
- ๋ชจ๋์นํฐํธ
- borderspacing
- liststyleimage
- formtag
- CDN
- texttransform
- Today
- Total
็ก์
Component (์ปดํฌ๋ํธ) ๋ณธ๋ฌธ
๐ก Component
React๋ ์ฌ์ฉ์ ์ธํฐํ์ด์ค(UI)๋ฅผ ๋ ๋ฆฝ์ ์ด๊ณ ์ฌ์ฌ์ฉ ๊ฐ๋ฅํ ๋จ์์ธ ์ปดํฌ๋ํธ๋ก ๋๋์ด ๊ฐ๋ฐํ๋ฉฐ, ์ด๋ฅผ ์กฐํฉํ์ฌ ํ๋์ ํ๋ฉด์ ๊ตฌ์ฑํ๋ค. ๊ฐ ์ปดํฌ๋ํธ๋ ์์ฒด UI์ ๊ธฐ๋ฅ์ ๊ฐ์ง๋ฉฐ ๋ค๋ฅธ ์ปดํฌ๋ํธ์ ๋ ๋ฆฝ์ ์ผ๋ก ๋์ํ๋ค.
โผ๏ธ ์ฃผ์ ํน์ง
- UI ๊ตฌ์ฑ: ํ๋ฉด์ ํน์ ์์ญ์ ๋ด๋นํ์ฌ ์๊ฐ์ ์์๋ฅผ ํ์ฑํ๋ค.
- ์ฌ์ฌ์ฉ์ฑ: ๋์ผํ ์ฝ๋๋ฅผ ์ฌ๋ฌ ์์น์์ ์ฌ์ฌ์ฉํ ์ ์์ด ์ค๋ณต์ ์ค์ธ๋ค.
- ์ ์ง๋ณด์์ฑ: ๊ธฐ๋ฅ๋ณ๋ก ์ฝ๋๊ฐ ๋ถ๋ฆฌ๋์ด ์์ด ์์ ๊ณผ ๊ด๋ฆฌ๊ฐ ์ฉ์ดํ๋ค.
- ๋ ๋ฆฝ์ฑ: ๊ฐ ์ปดํฌ๋ํธ๊ฐ ๋ ๋ฆฝ์ ์ผ๋ก ์๋ํ๋ฏ๋ก ๋ณ๊ฒฝ ์ฌํญ์ด ๋ค๋ฅธ ์์ญ์ ๋ฏธ์น๋ ์ํฅ์ ์ต์ํํ๋ค.
โผ๏ธ ์ปดํฌ๋ํธ์ ์ข ๋ฅ
๐น ํจ์ํ ์ปดํฌ๋ํธ (Functional Component)
- JavaScript ํจ์๋ก ์์ฑํ๋ ์ปดํฌ๋ํธ์ด๋ค.
- React Hooks๋ฅผ ์ง์ํจ์ ๋ฐ๋ผ ์ํ(State) ๊ด๋ฆฌ์ ์๋ช ์ฃผ๊ธฐ(Lifecycle) ๊ธฐ๋ฅ์ ๋ชจ๋ ๊ตฌํํ ์ ์์ด, ํ์ฌ React์ ํ์ค ์์ฑ ๋ฐฉ์์ด๋ค.
๐น ํด๋์คํ ์ปดํฌ๋ํธ (Class Component)
- JavaScript ํด๋์ค ๋ฌธ๋ฒ์ผ๋ก ์์ฑํ๋ฉฐ React.Component๋ฅผ ์์๋ฐ๋๋ค.
- ๊ณผ๊ฑฐ ์ํ ๊ด๋ฆฌ์ ์๋ช ์ฃผ๊ธฐ ๋ฉ์๋๋ฅผ ์ฌ์ฉํ๊ธฐ ์ํด ์ฐ์์ผ๋, Hooks ๋ฑ์ฅ ์ดํ ํ์ฌ๋ ๋๋ถ๋ถ ํจ์ํ ์ปดํฌ๋ํธ๋ก ๋์ฒด๋์๋ค.
โผ๏ธ ๋ช ๋ช ๊ท์น
React ์ปดํฌ๋ํธ์ ์ด๋ฆ์ ๋ฐ๋์ ๋๋ฌธ์๋ก ์์ํด์ผ ํ๋ค. (PascalCase)
// HTML DOM ํ๊ทธ๋ก ์ธ์
const element1 = <div />;
// React ์ปดํฌ๋ํธ๋ก ์ธ์
const element2 = <Welcome name="๋ฆฌ์กํธ" />; // Welcome์ ๋๋ฌธ์๋ก ์์
- ์๋ฌธ์๋ก ์์ํ๋ ํ๊ทธ๋ ํ์ค HTML ํ๊ทธ(div, span ๋ฑ)๋ก ์ธ์ํ๋ค.
- ๋๋ฌธ์๋ก ์์ํ๋ ํ๊ทธ๋ง ์ฌ์ฉ์ ์ ์ ์ปดํฌ๋ํธ๋ก ์ธ์ํ๋ค.
โผ๏ธ ์ปดํฌ๋ํธ์ Props
Props(Properties)๋ ๋ถ๋ชจ ์ปดํฌ๋ํธ๊ฐ ์์ ์ปดํฌ๋ํธ์ ๋ฐ์ดํฐ๋ฅผ ์ ๋ฌํ ๋ ์ฌ์ฉํ๋ ์๋จ์ด๋ค.
Props๋ฅผ ํ์ฉํ๋ฉด ๋์ผํ ์ปดํฌ๋ํธ ๊ตฌ์กฐ ๋ด์์ ์ ๋ฌ๋ฐ๋ ๋ฐ์ดํฐ์ ๋ฐ๋ผ ์๋ก ๋ค๋ฅธ ๋ด์ฉ์ ์ถ๋ ฅํ ์ ์๋ค.
<Button text="ํ์ธ" />
<Button text="์ทจ์" />
<Button text="์ ์ฅ" />
- ์ ๋ฌ๋ฐ๋ text ๊ฐ์ ๋ฐ๋ผ ๊ฐ๊ธฐ ๋ค๋ฅธ ๋ฒํผ์ ์ถ๋ ฅํ๋ค.
โผ๏ธ ์ปดํฌ๋ํธ ์กฐํฉ(Composition)
์ฌ๋ฌ ๊ฐ์ ์์ ์ปดํฌ๋ํธ๋ฅผ ๊ฒฐํฉํ์ฌ ํ๋์ ํฐ ํ๋ฉด์ ๊ตฌ์ฑํ๋ ๋ฐฉ์์ ์ปดํฌ๋ํธ ์กฐํฉ์ด๋ผ๊ณ ํ๋ค.
App
โโโ Navbar
โ โโโ NavLink
โโโ Heading
โโโ Card
โ โโโ Heading
โ โโโ Button
โโโ Image
- ์์ ๋จ์์ ์ปดํฌ๋ํธ๋ฅผ ์กฐํฉํ์ฌ ๊ตฌ์กฐ๋ฅผ ๊ณ์ธตํํ๋ฉด ์ ์ฒด์ ์ธ ์ฝ๋์ ๊ฐ๋ ์ฑ๊ณผ ์ฌ์ฌ์ฉ์ฑ์ด ๋์์ง๋ค.
๐โ POINT ๐โ
โ๏ธ UI๋ฅผ ๋ ๋ฆฝ์ ์ด๊ณ ์ฌ์ฌ์ฉ ๊ฐ๋ฅํ ๋จ์๋ก ๋ถ๋ฆฌํ์ฌ ๊ฐ๋ฐ ํจ์จ์ฑ์ ๊ทน๋ํํ๋ค. โ๏ธ
โ๏ธ ์ปดํฌ๋ํธ ๊ธฐ๋ฐ ์ค๊ณ๋ฅผ ๋ฐํ์ผ๋ก Props ๋ฐ์ดํฐ๋ฅผ ํ์ฉํ๊ณ ์ ์ ํ ์กฐํฉ(Composition)ํ๋ ๊ฒ์ด React ์ ํ๋ฆฌ์ผ์ด์ ๊ฐ๋ฐ์ ๊ธฐ๋ณธ ๊ตฌ์กฐ์ด๋ค. โ๏ธ
โ๏ธ ์ค์ ์์ ์ฝ๋ โ๏ธ
๐ก ์ค์ ์์ : ๋ฐฐ์ด ๋ ๋๋ง ์ key ์ค์ map() ํจ์๋ฅผ ํตํด ์ปดํฌ๋ํธ๋ฅผ ๋ฐ๋ณต ์์ฑํ ๋๋ ๊ณ ์ ํ key ์์ฑ์ ์ง์ ํด์ผ ํ๋ค.
React๊ฐ ๋ณ๊ฒฝ๋ ์์๋ฅผ ์ ํํ๊ฒ ์๋ณํ๊ณ ๊ฐ์ DOM์ ํจ์จ์ ์ผ๋ก ๋ ๋๋งํ๋ ๋ฐ ํ์ํ๋ค.
import React from 'react';
// 1. ๊ธฐ๋ณธ ๋ฒํผ ์ปดํฌ๋ํธ
function Button(props) {
return <button className="custom-button">{props.text}</button>;
}
// 2. ์ ๋ชฉ ์ปดํฌ๋ํธ
function Heading(props) {
return <h1 className="title">{props.title}</h1>;
}
// 3. ์นด๋ ์ปดํฌ๋ํธ (Heading, Button ์กฐํฉ)
function Card(props) {
return (
<div className="card">
<Heading title={props.title} />
<p className="card-content">{props.content}</p>
<Button text={props.buttonText} />
</div>
);
}
// 4. ์ด๋ฏธ์ง ์ปดํฌ๋ํธ
function Image(props) {
return <img src={props.src} alt={props.alt} className="custom-image" />;
}
// 5. ๋ค๋น๊ฒ์ด์
๋งํฌ ์ปดํฌ๋ํธ
function NavLink(props) {
return <a href={props.url} className="nav-link">{props.text}</a>;
}
// 6. ๋ค๋น๊ฒ์ด์
๋ฐ ์ปดํฌ๋ํธ
function Navbar(props) {
return (
<nav className="navbar">
<ul>
{/* ๋ฐฐ์ด์ ๋ ๋๋งํ ๋๋ ๊ณ ์ ํ key ์์ฑ์ด ํ์ํฉ๋๋ค */}
{props.links.map((link, index) => (
<li key={index}>
<NavLink url={link.url} text={link.text} />
</li>
))}
</ul>
</nav>
);
}
// 7. App ์ปดํฌ๋ํธ (์ต์์ ์กฐํฉ)
function App() {
const navLinks = [
{ url: "/home", text: "ํ" },
{ url: "/about", text: "์๊ฐ" },
{ url: "/contact", text: "์ฐ๋ฝ์ฒ" }
];
return (
<div className="app">
<Navbar links={navLinks} />
<div className="main-content">
<Heading title="React ์ปดํฌ๋ํธ ์์ " />
<Card
title="์ฒซ ๋ฒ์งธ ์นด๋"
content="์ด๊ฒ์ React ์ปดํฌ๋ํธ๋ก ๋ง๋ ์นด๋์
๋๋ค."
buttonText="์์ธํ ๋ณด๊ธฐ"
/>
<Card
title="๋ ๋ฒ์งธ ์นด๋"
content="์ปดํฌ๋ํธ๋ฅผ ์ฌ์ฌ์ฉํ๋ฉด ์ฝ๋๋ฅผ ํจ์จ์ ์ผ๋ก ๊ด๋ฆฌํ ์ ์์ต๋๋ค."
buttonText="ํ์ธํ๊ธฐ"
/>
<Image
src="https://example.com/image.jpg"
alt="์์ ์ด๋ฏธ์ง"
/>
</div>
</div>
);
}
export default App;
'<React>' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
| Event (์ด๋ฒคํธ) (0) | 2026.07.08 |
|---|---|
| Props (Properties, ํ๋กญ์ค) (0) | 2026.07.07 |
| Create React App(CRA) tool chain (0) | 2026.07.06 |
| VS Code์์ React ํ๋ก์ ํธ ์์ฑ (0) | 2026.07.06 |
| Node.js ์ค์น์ CMD&Powershell ์ฌ์ฉ (0) | 2026.07.06 |
