๋ณธ๋ฌธ์œผ๋กœ ๋ฐ”๋กœ๊ฐ€๊ธฐ

[React] 6. Components and Props

category Web/React 2023. 11. 5. 15:43

6. Components and Props

Components

  • A, B๋Š” ๋ฆฌ์•กํŠธ ์ปดํฌ๋„ŒํŠธ์ด๋‹ค.

  • ์ด๋Ÿฌํ•œ ์ปดํฌ๋„ŒํŠธ๋ฅผ ์—ฌ๋Ÿฌ๋ฒˆ ์‚ฌ์šฉํ•ด์„œ ํ•˜๋‚˜์˜ ํŽ˜์ด์ง€๋ฅผ ๊ตฌ์„ฑํ•˜๊ณ  ์žˆ๋‹ค.
  • ์ž‘์€ ์ปดํฌ๋„ŒํŠธ๋“ค์ด ๋ชจ์—ฌ ํ•˜๋‚˜์˜ ์ปดํฌ๋„ŒํŠธ๋ฅผ ๊ตฌ์„ฑํ•˜๊ณ , ์ด๋Ÿฌํ•œ ์ปดํฌ๋„ŒํŠธ๋“ค์ด ๋ชจ์—ฌ ์ „์ฒด ํŽ˜์ด์ง€๋ฅผ ๊ตฌ์„ฑํ•œ๋‹ค.
  • ๋ฆฌ์•กํŠธ ์ปดํฌ๋„ŒํŠธ๋ฅผ ํ•˜๋‚˜์˜ ํ•จ์ˆ˜๋ผ๊ณ  ์ƒ๊ฐํ•˜๋ฉด ์‰ฝ๋‹ค. ํ•˜์ง€๋งŒ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์˜ ํ•จ์ˆ˜์™€๋Š” ์•ฝ๊ฐ„ ๋‹ค๋ฅด๋‹ค.

๋ฆฌ์•กํŠธ์ปดํฌ๋„ŒํŠธ๋Š” ์†์„ฑProps๋ฅผ ๋„ฃ์œผ๋ฉด ์—˜๋ฆฌ๋จผํŠธ๋ฅผ ๋ฆฌํ„ดํ•œ๋‹ค.

๊ฐ์ฒด์ง€ํ–ฅ์˜ ์˜๋ฏธ์™€ ๋น„์Šทํ•˜๋‹ค.

Props

  • prop์ด ์—ฌ๋Ÿฌ ๊ฐœ์ธ ๊ฒƒ.
  • prop : property, ์†์„ฑ (์—˜๋ฆฌ๋จผํŠธ๋ฅผ ๋งŒ๋“ค๊ธฐ ์œ„ํ•œ ์žฌ๋ฃŒ)
    • ์ปดํฌ๋„ŒํŠธ์˜ ์†์„ฑ
    • ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๊ฐ์ฒด
์ปดํฌ๋„ŒํŠธ : props๋ฅผ ๋ฐ›์•„ ์—˜๋ฆฌ๋จผํŠธ๋ฅผ ๋ฆฌํ„ดํ•˜๋Š” ํ•จ์ˆ˜
์—˜๋ฆฌ๋จผํŠธ : ๋ณด์ด๋Š” ๋ถ€๋ถ„
props : ์ปดํฌ๋„ŒํŠธ์— ์ „๋‹ฌํ•  ์ •๋ณด๋ฅผ ๋‹ด๊ณ  ์žˆ๋Š” ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๊ฐ์ฒด

 

 

Props์˜ ํŠน์ง•

  1. Read-Only : ๊ฐ’์„ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์—†๋‹ค.
    • ์—˜๋ฆฌ๋จผํŠธ ์ƒ์„ฑ ๋„์ค‘ ๋ฐ”๋€Œ์–ด๋ฒ„๋ฆฌ๋ฉด ์•ˆ๋˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.
javascript ํ•จ์ˆ˜์˜ ํŒŒ๋ผ๋ฏธํ„ฐ = ๋ฆฌ์•กํŠธ ์ปดํฌ๋„ŒํŠธ์˜ ํ”„๋ž์Šค
  1. ๋ฆฌ์•กํŠธ์ปดํฌ๋„ŒํŠธ ํ”„๋ž์Šค๋Š” ๋ฐ”๊ฟ€ ์ˆ˜ ์—†๊ณ , ๊ฐ™์€ ํ”„๋ž์Šค์— ๋Œ€ํ•ด ๊ฐ™์€ ๊ฒฐ๊ณผ๊ฐ€ ๋‚˜์™€์•ผ ํ•œ๋‹ค.
  2. ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๊ฐ์ฒด

 

Component ๋งŒ๋“ค๊ธฐ

  • ํด๋ž˜์Šค ์ปดํฌ๋„ŒํŠธ
  • ํ•จ์ˆ˜ ์ปดํฌ๋„ŒํŠธ

๋ฆฌ์•กํŠธ์ปดํฌ๋„ŒํŠธ๋Š” ํด๋ž˜์Šค ์ปดํฌ๋„ŒํŠธ, ํ•จ์ˆ˜ ์ปดํฌ๋„ŒํŠธ 2๊ฐ€์ง€๋กœ ๋‚˜๋‰œ๋‹ค.

๋ฆฌ์•กํŠธ ์ดˆ๊ธฐ๋ฒ„์ „์€ ํด๋ž˜์Šค ์ปดํฌ๋„ŒํŠธ๋ฅผ ์ฃผ๋กœ ์‚ฌ์šฉํ•˜์˜€์œผ๋‚˜ ์‚ฌ์šฉํ•˜๊ธฐ ๋ถˆํŽธํ–ˆ๋‹ค.

๋•Œ๋ฌธ์— ํ•จ์ˆ˜์ปดํฌ๋„ŒํŠธ๋ฅผ ๊ฐœ์„ ํ•ด์„œ ์‚ฌ์šฉํ•˜์˜€๊ณ  ์ดํ›„์—๋Š” ‘Hook’์„ ์‚ฌ์šฉํ•˜๊ฒŒ ๋˜์—ˆ๋‹ค.

ํ˜„์žฌ๋Š” ๋Œ€๋ถ€๋ถ„ Hook์„ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ๋‹ค.

Function Component

Class Component

 

 

Component์˜ ์ด๋ฆ„

  1. ์ปดํฌ๋„ŒํŠธ์˜ ์ด๋ฆ„์€ ํ•ญ์ƒ ๋Œ€๋ฌธ์ž๋กœ ์‹œ์ž‘ํ•ด์•ผ ํ•œ๋‹ค. (์‚ฌ์šฉ์ž๊ฐ€ ์ •์˜ํ•œ ์ปดํฌ๋„ŒํŠธ๋ผ๋Š” ์˜๋ฏธ)
  • ๋ฆฌ์•กํŠธ๋Š” ์†Œ๋ฌธ์ž๋กœ ์‹œ์ž‘ํ•˜๋Š” ์ปดํฌ๋„ŒํŠธ๋Š” 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