JiYoung Dev πŸ–₯

[React] Component, Props, State (2023.05.16) λ³Έλ¬Έ

full stack/React

[React] Component, Props, State (2023.05.16)

Shinjio 2023. 5. 16. 18:04

terminal λͺ…λ Ήμ–΄

npm : 이미 μ„€μΉ˜κ°€ λ˜μ–΄μžˆμ„ 아이λ₯Ό μ‚¬μš©ν•  λ•Œ (ex. terminal start)

npx : μ„€μΉ˜κ°€ λ˜μ–΄μžˆμ§€μ•ŠλŠ” 것을 μ‚¬μš©ν•  λ•Œ (ex. npx create-react-app project02)

μ’…λ£Œ : ctrl + C

 

βš™ project 폴더 ꡬ성

node_modules : ν•„μš”ν•œ μž₯μΉ˜λ“€

public : μ •μ μΈ 파일 / μ΄λ―Έμ§€, html, json, text λ“±

src : js, css λ“± λ™μ μœΌλ‘œ λ™μ ν•˜κΈ° μœ„ν•΄ ν•„μš”ν•œ μΉœκ΅¬λ“€

 

🎈 μ»΄ν¬λ„ŒνŠΈ

λ¦¬μ•‘νŠΈλŠ” ν™”λ©΄μ—μ„œ UI μš”μ†Œλ₯Ό ꡬ뢄할 λ•Œ 'μ»΄ν¬λ„ŒνŠΈ'λΌλŠ” λ‹¨μœ„λ₯Ό μ‚¬μš©ν•¨

μ»€λ‹€λž€ λ‹¨μœ„, κ°œλ°œμžκ°€ μ»€μŠ€ν…€ν•  수 μžˆλŠ” μ΅œμ†Œ λ‹¨μœ„ (κΈ°λŠ₯별 ν˜Ήμ€ νŽ˜μ΄μ§€λ³„λ‘œ λ§Œλ“œλŠ” κ²½μš°κ°€ 많음)

λ‚΄κ°€ μ›ν•˜λŠ” νƒœκ·Έλ“€μ„ λͺ¨μ•„μ„œ ν•˜λ‚˜μ˜ λ‹¨μœ„λ‘œ λ§Œλ“€μ–΄μ€€λ‹€.

 

 

 

 

react 문법 μž‘μ„±μ‹œ μœ μš©ν•œ 도ꡬ

 

πŸ“– μ»΄ν¬λ„ŒνŠΈ 생성

μ»΄ν¬λ„ŒνŠΈλŠ” 폴더에 λ”°λ‘œ 관리 

 

 

μ»΄ν¬λ„ŒνŠΈ λ§Œλ“€ λ•Œ κΌ­ λŒ€λ¬Έμžλ‘œ μ‹œμž‘! - λ¦¬μ•‘νŠΈμ˜ ν”„λ ˆμž„μ›Œν¬μ μΈ 성격

(μ†Œλ¬Έμžλ‘œ μ‹œμž‘ν•˜λŠ” HTML νƒœκ·Έ(DOM μš”μ†Œ)와 κ΅¬λΆ„ν•˜κΈ° μœ„ν•΄!)

ν™•μž₯μžλŠ” js둜 해도 상관 μ—†μŒ. 

 

μˆ˜μž…(import), 수좜(export default) μž‘μ—…μ΄ ν•„μˆ˜!

 

βš™ props

μƒμœ„ μ»΄ν¬λ„ŒνŠΈμ—μ„œ ν•˜μœ„ μ»΄ν¬λ„ŒνŠΈλ‘œ 값을 보내쀄 λ•Œ μ‚¬μš©

μƒμœ„ μ»΄ν¬λ„ŒνŠΈμ—μ„œλŠ” 속성 = "κ°’"

                                  ν˜Ήμ€ λ³€μˆ˜ λ§Œλ“€μ–΄μ„œ 속성 = {λ³€μˆ˜}

ν•˜μœ„ μ»΄ν¬λ„ŒνŠΈμ—μ„œλŠ” λ§€κ°œλ³€μˆ˜μ— 값을 λ°›μ•„μ™”λ‹€

받을 수 μžˆλŠ” Case1) λ§€κ°œλ³€μˆ˜μ— props => props.name

                     Case2) λ§€κ°œλ³€μˆ˜μ— {name, gender} => name ** 비ꡬ쑰화 ν• λ‹Ή κ°œλ…

 

 

객체 비ꡬ쑰화 ν• λ‹Ή

λ³€μˆ˜μ™€ 속성이름이 κ°™λ‹€λ©΄ ν•˜λ‚˜μ˜ λ‹¨μ–΄λ‘œ μ‚¬μš© κ°€λŠ₯

 

let props = {

    name=""

    price=""

}

let {name, price} = {

    name=""

    price=""

}

 

AppExample.js νŒŒμΌμ—μ„œ MemberBox μ»΄ν¬λ„ŒνŠΈ λŒμ–΄μ“°κΈ°

// rafce(react arrow function export component) + Enter : 단좕킀

import React from "react";
import "./App.css";
import MemberBox from "./components/MemberBox";

const AppExample = () => {
  /* μ—¬λŸ¬λΆ„μ˜ νŒ€μ›μ„ μ†Œκ°œν•΄μ£Όμ„Έμš”!
이름 : 홍길동
성별 : μ—¬μ„±/남성
μ’‹μ•„ν•˜λŠ” λ…Έλž˜ :  μ•„μ΄λΈŒ - I AM

단, Component와 props의 κ°œλ…μ„ μ΄μš©ν•  것
- MemberBox λΌλŠ” μ»΄ν¬λ„ŒνŠΈλ₯Ό μƒμ„±ν•˜κΈ°

ν˜Ήμ‹œ, λ„ˆλ¬΄ 빨리 끝내버린 μ‚¬λžŒμ€ λ””μžμΈν•˜κ³  있기!
*/
  let { man, woman } = {
    man: "남성",
    woman: "μ—¬μ„±",
  };
  return (
    <div>
      <MemberBox name="μ‹ μ§€μ˜" gender={woman} song="μ§€μˆ˜ - 꽃"></MemberBox>
      <MemberBox
        name="μ„œν˜„λ‘"
        gender={woman}
        song="λ‰΄μ§„μŠ€ - Hypeboy"
      ></MemberBox>
      <MemberBox name="κΉ€μ‹ μ˜" gender={man} song="λ‰΄μ§„μŠ€ - ditto"></MemberBox>
      <MemberBox
        name="μ˜μ„"
        gender={man}
        song="μ•„μ΄μœ  - 잠λͺ»λ“œλŠ”λ°€μ—λΉ„λŠ”λ‚΄λ¦¬κ³ "
      ></MemberBox>
    </div>
  );
};

export default AppExample;

 

MemberBox μ»΄ν¬λ„ŒνŠΈ

const MemberBox = ({name, gender, song})=>{
    return(
        <div className="member-box">
        <div id="name">{name}</div>
        <div>{gender}</div>
        <div>{song}</div>
        </div>
    )
}

export default MemberBox

 

λ‚΄κ°€λ§Œλ“  μ»΄ν¬λ„ŒνŠΈμ—λŠ” μ§μ ‘μ μœΌλ‘œ λ””μžμΈμ„ μž…νž 수 μ—†μŒ

μ»΄ν¬λ„ŒνŠΈ λ‚΄ html μš”μ†Œμ— λ””μžμΈμ„ μ£Όλ©΄ 됨

 

βš™ state

 

λ³€ν™”κ°€ 있으면 화면에 λ°”λ‘œ λ Œλ”λ§ ν•΄μ€˜μ•Ό ν•  λ•Œ μ‚¬μš©

 

 

import {useState} from 'react'
const [λ³€μˆ˜μ΄λ¦„, setλ³€μˆ˜μ΄λ¦„] = useState(μ΄ˆκΈ°κ°’)

 

import React from "react";
import { useState } from "react";

const Ex01 = () => {
  /* React Event
  1. onClick = {ν•¨μˆ˜μ΄λ¦„}
      μ΄λ•Œ, JS처럼 ()λ₯Ό 뒀에 뢙이면 λ°”λ‘œ 호좜
      {()=>{ν•¨μˆ˜μ΄λ¦„()}} 도 μ‚¬μš© κ°€λŠ₯
  
  2. DOM μš”μ†Œμ—λ§Œ 이벀트λ₯Ό 걸어쀄 수 μžˆλ”°. 
    λ‚΄κ°€ λ§Œλ“  μ»΄ν¬λ„ŒνŠΈμ—λŠ” 이벀트λ₯Ό κ±Έ 수 μ—†μŒ
  */

  /* State
  - μ»΄ν¬λ„ŒνŠΈ λ‚΄λΆ€μ—μ„œ κ΄€λ¦¬ν•˜λŠ” 변경이 κ°€λŠ₯ν•œ 데이터
  - λ³€μˆ˜μ™€ λ‹€λ₯Έμ ? 화면에 λ Œλ”λ§λœλ‹€λŠ” 점
    JSμ—μ„œ 값이 λ³€ν•˜λŠ” 과정을 μƒκ°ν•˜λ©΄ λ§ˆμ§€λ§‰μ— 화면에 λ Œλ”λ§μ„ μ‹œμΌœμ€˜μ•Ό 함
    => useStateλΌλŠ” ν•¨μˆ˜λ₯Ό μ‚¬μš©ν•΄μ€„κ²ƒ!
    
    const [λ³€μˆ˜μ΄λ¦„, setλ³€μˆ˜μ΄λ¦„] = useState(μ΄ˆκΈ°κ°’)
    - λ³€μˆ˜μ΄λ¦„
    - setλ³€μˆ˜μ΄λ¦„ : λ³€μˆ˜μ΄λ¦„μ„ μˆ˜μ •ν•΄μ•Όν•  λ•Œ μ‚¬μš©ν•  ν•¨μˆ˜ 이름
                    num = 2(X)    => λ³€μˆ˜λ§Œ λ°”λ€œ, 화면은 κ·ΈλŒ€λ‘œ
                    setNum(2) (O) => ν™”λ©΄κΉŒμ§€ 같이 값이 λ³€ν™”
    - useState : import μž‘μ—…μ΄ ν•„μš”ν•¨
                  import {useState} from 'react'
  */

  let num = 0;
  const [num2, setNum2] = useState(0);

  const plus = () => {
    num += 1;
    console.log("ν˜„μž¬ μˆ«μžλŠ” : ", num);

    setNum2(num2 + 1);
  };

  const minus = () => {
    setNum2(num2 - 1);
  };

  return (
    <div>
      <p>λ‹¨μˆœ λ³€μˆ˜ : {num}</p>
      {/* ν™”λ©΄ λžœλ”λ§κΉŒμ§€ 이어지지 μ•ŠμŒ -> useState() μ‚¬μš©ν•˜λŠ” 이유 */}
      <p>state : {num2}</p>
      <button onClick={plus}>+1</button>
      {/* μ†Œκ΄„ν˜Έλ₯Ό μ“°κ²Œλ˜λ©΄ νŽ˜μ΄μ§€κ°€ μ‹€ν–‰λ λ•Œλ§ˆλ‹€ μžλ™μœΌλ‘œ ν•¨μˆ˜κ°€ 호좜됨 */}
      <button
        onClick={() => {
          setNum2(num2 - 1); 
          // 읡λͺ…ν•¨μˆ˜
        }}
      >
        -1
      </button>
    </div>
  );
};

export default Ex01;

 

였λ₯˜ κ΄€λ ¨ 정리

 

λ¦¬μ•‘νŠΈλŠ” 3000 포트 μ‚¬μš©

이 경우 μ„œλ²„κ°€ 2개 λŒμ•„κ°€λŠ” 쀑 (였λ₯Έμͺ½ node 2개)

싀행쀑인 μ„œλ²„λ₯Ό μ’…λ£Œν•˜κ±°λ‚˜ (Ctrl + C)

ν˜Ήμ€ Yλ₯Ό λˆŒλŸ¬μ„œ μƒˆλ‘œμš΄ 포트둜 μƒˆλ‘œμš΄ μ„œλ²„λ₯Ό μ‹€ν–‰ν•˜κ±°λ‚˜