6. Components and Props
Components
- A, B๋ ๋ฆฌ์กํธ ์ปดํฌ๋ํธ์ด๋ค.
- ์ด๋ฌํ ์ปดํฌ๋ํธ๋ฅผ ์ฌ๋ฌ๋ฒ ์ฌ์ฉํด์ ํ๋์ ํ์ด์ง๋ฅผ ๊ตฌ์ฑํ๊ณ ์๋ค.
- ์์ ์ปดํฌ๋ํธ๋ค์ด ๋ชจ์ฌ ํ๋์ ์ปดํฌ๋ํธ๋ฅผ ๊ตฌ์ฑํ๊ณ , ์ด๋ฌํ ์ปดํฌ๋ํธ๋ค์ด ๋ชจ์ฌ ์ ์ฒด ํ์ด์ง๋ฅผ ๊ตฌ์ฑํ๋ค.
- ๋ฆฌ์กํธ ์ปดํฌ๋ํธ๋ฅผ ํ๋์ ํจ์๋ผ๊ณ ์๊ฐํ๋ฉด ์ฝ๋ค. ํ์ง๋ง ์๋ฐ์คํฌ๋ฆฝํธ์ ํจ์์๋ ์ฝ๊ฐ ๋ค๋ฅด๋ค.
๋ฆฌ์กํธ์ปดํฌ๋ํธ๋ ์์ฑProps๋ฅผ ๋ฃ์ผ๋ฉด ์๋ฆฌ๋จผํธ๋ฅผ ๋ฆฌํดํ๋ค.
๊ฐ์ฒด์งํฅ์ ์๋ฏธ์ ๋น์ทํ๋ค.
Props
- prop์ด ์ฌ๋ฌ ๊ฐ์ธ ๊ฒ.
- prop : property, ์์ฑ (์๋ฆฌ๋จผํธ๋ฅผ ๋ง๋ค๊ธฐ ์ํ ์ฌ๋ฃ)
- ์ปดํฌ๋ํธ์ ์์ฑ
- ์๋ฐ์คํฌ๋ฆฝํธ ๊ฐ์ฒด
์ปดํฌ๋ํธ : props๋ฅผ ๋ฐ์ ์๋ฆฌ๋จผํธ๋ฅผ ๋ฆฌํดํ๋ ํจ์
์๋ฆฌ๋จผํธ : ๋ณด์ด๋ ๋ถ๋ถ
props : ์ปดํฌ๋ํธ์ ์ ๋ฌํ ์ ๋ณด๋ฅผ ๋ด๊ณ ์๋ ์๋ฐ์คํฌ๋ฆฝํธ ๊ฐ์ฒด
Props์ ํน์ง
- Read-Only : ๊ฐ์ ๋ณ๊ฒฝํ ์ ์๋ค.
- ์๋ฆฌ๋จผํธ ์์ฑ ๋์ค ๋ฐ๋์ด๋ฒ๋ฆฌ๋ฉด ์๋๊ธฐ ๋๋ฌธ์ด๋ค.
javascript ํจ์์ ํ๋ผ๋ฏธํฐ = ๋ฆฌ์กํธ ์ปดํฌ๋ํธ์ ํ๋์ค
- ๋ฆฌ์กํธ์ปดํฌ๋ํธ ํ๋์ค๋ ๋ฐ๊ฟ ์ ์๊ณ , ๊ฐ์ ํ๋์ค์ ๋ํด ๊ฐ์ ๊ฒฐ๊ณผ๊ฐ ๋์์ผ ํ๋ค.
- ์๋ฐ์คํฌ๋ฆฝํธ ๊ฐ์ฒด
Component ๋ง๋ค๊ธฐ
- ํด๋์ค ์ปดํฌ๋ํธ
- ํจ์ ์ปดํฌ๋ํธ
๋ฆฌ์กํธ์ปดํฌ๋ํธ๋ ํด๋์ค ์ปดํฌ๋ํธ, ํจ์ ์ปดํฌ๋ํธ 2๊ฐ์ง๋ก ๋๋๋ค.
๋ฆฌ์กํธ ์ด๊ธฐ๋ฒ์ ์ ํด๋์ค ์ปดํฌ๋ํธ๋ฅผ ์ฃผ๋ก ์ฌ์ฉํ์์ผ๋ ์ฌ์ฉํ๊ธฐ ๋ถํธํ๋ค.
๋๋ฌธ์ ํจ์์ปดํฌ๋ํธ๋ฅผ ๊ฐ์ ํด์ ์ฌ์ฉํ์๊ณ ์ดํ์๋ ‘Hook’์ ์ฌ์ฉํ๊ฒ ๋์๋ค.
ํ์ฌ๋ ๋๋ถ๋ถ Hook์ ์ฌ์ฉํ๊ณ ์๋ค.
Function Component
Class Component
Component์ ์ด๋ฆ
- ์ปดํฌ๋ํธ์ ์ด๋ฆ์ ํญ์ ๋๋ฌธ์๋ก ์์ํด์ผ ํ๋ค. (์ฌ์ฉ์๊ฐ ์ ์ํ ์ปดํฌ๋ํธ๋ผ๋ ์๋ฏธ)
- ๋ฆฌ์กํธ๋ ์๋ฌธ์๋ก ์์ํ๋ ์ปดํฌ๋ํธ๋ domํ๊ทธ๋ก ์ธ์ํ๊ธฐ ๋๋ฌธ์ด๋ค.
- div, span๋ ๋ด์ฅ ์ปดํฌ๋ํธ. ⇒ React.createElement() ์ ์ ๋ฌ๋จ.
- ๋๋ฌธ์๋ก ์์ํ๋ ์ปดํฌ๋ํธ ⇒ React.createElementFoo ํํ๋ก ์ปดํ์ผ๋จ. ⇒ ์๋ฐ์คํฌ๋ฆฝํธ ํ์ผ ๋ด์์ ์ฌ์ฉ์๊ฐ ์ ์ํ๊ฑฐ๋ importํ ์ปดํฌ๋ํธ๋ฅผ ๊ฐ๋ฆฌํจ๋ค.
Component ๋ ๋๋ง
function Welcome(props) {
return <h1>์๋
, {props.name}!</h1>;
}
const root = ReactDOM.createRoot(document.getElementById());
const element = <Welcome name="์๋" />;
root.render(element);
Component ํฉ์ฑ
Component ์ถ์ถ
์ถ์ถ ์
์ถ์ถ ํ
๋๊ธ ์ปดํฌ๋ํธ
Comment.jsx
import React from "react";
const styles = {
wrapper: {
margin: 8,
padding: 8,
display: "flex",
flexDirection: "row",
border: "1px solid grey",
borderRadius: 16,
},
imageContainer: {},
image: {
width: 50,
height: 50,
borderRadius: 25,
},
contentContainer: {
marginLeft: 8,
display: "flex",
flexDirection: "column",
justifyContent: "center",
},
nameText: {
color: "black",
fontSize: 16,
fontWeight: "bold",
},
commentText: {
color: "black",
fontSize: 16,
},
};
function Comment(props) {
return (
<div style={styles.wrapper}>
<div style={styles.imageContainer}>
<img src="https://cdn-icons-png.flaticon.com/512/6522/6522516.png" style={styles.image} />
</div>
<div styles={styles.contentContainer}>
<span style={styles.nameText}>{props.name}</span>
<br />
<span style={styles.commentText}> {props.content}</span>
</div>
</div>
);
}
export default Comment;
CommentList.jsx
import React from "react";
import Comment from "./Comment";
const comments = [
{
name: "์๋๊ฐ",
content: "์๋
ํ์ธ์ฉ!",
},
{
name: "๊ฐ์๋๊ฐ",
content: "๋ฐ๊ฐ์ต๋๋ฐ์~",
},
];
function CommentList(props) {
return (
<div>
{comments.map((comment) => {
return <Comment name={comment.name} content={comment.content} />;
})}
</div>
);
}
export default CommentList;
index.js
import React from "react";
import ReactDOM from "react-dom/client";
import "./index.css";
//import App from "./App";
import reportWebVitals from "./reportWebVitals";
//import Library from "./chapter_03/Library";
import Clock from "./chapter_04/Clock";
import CommentList from "./chapter_05/CommentList";
const root = ReactDOM.createRoot(document.getElementById("root"));
root.render(
<React.StrictMode>
<CommentList />
</React.StrictMode>
);
reportWebVitals();
๊ฒฐ๊ณผ
'Web > React' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[React] 8. Hooks (0) | 2023.11.06 |
---|---|
[React] 7. State and Lifecycle (0) | 2023.11.05 |
[React] Error: react_dom_client__WEBPACK_IMPORTED_MODULE_1__.render is not a function (0) | 2023.11.05 |
[React] 6. Rendering Elements (0) | 2023.11.05 |
[React] Error: Already included file name (0) | 2023.11.05 |