Jieunny์˜ ๋ธ”๋กœ๊ทธ

S2) Unit 6. [React] React State & Props ๋ณธ๋ฌธ

CodeStates/learning contents

S2) Unit 6. [React] React State & Props

Jieunny 2023. 1. 26. 12:00

๐Ÿ“ฃ  State

โœ”๏ธ ์ปดํฌ๋„ŒํŠธ ์‚ฌ์šฉ ์ค‘ ์ปดํฌ๋„ŒํŠธ ๋‚ด๋ถ€์—์„œ ๋ณ€ํ•  ์ˆ˜ ์žˆ๋Š” ๊ฐ’

โœ”๏ธ ๋‚ด๋ถ€์—์„œ ๋ณ€ํ™”ํ•˜๋Š” ๊ฐ’

๐Ÿ“ฃ  State hook, useState ์‚ฌ์šฉ๋ฒ•

โœ”๏ธ useState()

 

1๏ธโƒฃ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ

import { useState } from "react";

 

2๏ธโƒฃ ์ปดํฌ๋„ŒํŠธ ์•ˆ์—์„œ ํ˜ธ์ถœํ•˜๊ธฐ -> state๋ผ๋Š” ๋ณ€์ˆ˜๋ฅผ ์„ ์–ธํ•˜๋Š” ๊ฒƒ๊ณผ ๊ฐ™๋‹ค(state๋ณ€์ˆ˜๋Š” ํ•จ์ˆ˜๊ฐ€ ๋๋‚˜๋„ ์‚ฌ๋ผ์ง€์ง€ ์•Š๋Š”๋‹ค)

function CheckboxExample() {
// ์ƒˆ๋กœ์šด state ๋ณ€์ˆ˜๋ฅผ ์„ ์–ธํ•˜๊ณ , ์—ฌ๊ธฐ์„œ๋Š” ์ด๊ฒƒ์„ isChecked ๋ผ ๋ถ€๋ฅด๊ฒ ์Šต๋‹ˆ๋‹ค.
  const [isChecked, setIsChecked] = useState(false);
  // useState์˜ ๋ฆฌํ„ด๊ฐ’์„ ๊ตฌ์กฐ ๋ถ„ํ•ด ํ• ๋‹น ํ•˜๊ณ  ์žˆ๋‹ค.
  // setIsCheckedํ•จ์ˆ˜๋Š” isChecked ๋ณ€์ˆ˜๋ฅผ ๋ณ€๊ฒฝํ•˜๋Š” ํ•จ์ˆ˜
}
const [state ์ €์žฅ ๋ณ€์ˆ˜, state ๊ฐฑ์‹  ํ•จ์ˆ˜] = useState(์ƒํƒœ ์ดˆ๊ธฐ ๊ฐ’);

 

3๏ธโƒฃ ์‚ฌ์šฉํ•˜๊ธฐ

<span>{isChecked ? "Checked!!" : "Unchecked"}</span>

 

4๏ธโƒฃ ๊ฐฑ์‹ ํ•˜๊ธฐ

const handleChecked = (event) => {
    setIsChecked(event.target.checked);
  };

โžฐ setIsChecked ํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด isChecked ๋ณ€์ˆ˜๋ฅผ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ๋‹ค.

 

๐Ÿšจ ์ฃผ์˜์ 

โžฐ React ์ปดํฌ๋„ŒํŠธ๋Š” state๊ฐ€ ๋ณ€๊ฒฝ๋˜๋ฉด ์ƒˆ๋กญ๊ฒŒ ํ˜ธ์ถœ๋˜๊ณ , ๋ฆฌ๋ Œ๋”๋ง ๋œ๋‹ค.

โžฐ state๋Š” ์ƒํƒœ ๋ณ€๊ฒฝ ํ•จ์ˆ˜ ํ˜ธ์ถœ๋กœ ๋ณ€๊ฒฝํ•ด์•ผ ํ•œ๋‹ค. (๊ฐ•์ œ๋กœ ๋ณ€๊ฒฝX)

 

๐Ÿ“ฃ  Props

โœ”๏ธ ์™ธ๋ถ€๋กœ๋ถ€ํ„ฐ ์ „๋‹ฌ๋ฐ›์€ ๊ฐ’

โœ”๏ธ ๋ถ€๋ชจ ์ปดํฌ๋„ŒํŠธ๋กœ๋ถ€ํ„ฐ ์ „๋‹ฌ๋ฐ›์€ ๊ฐ’

โœ”๏ธ ๊ฐ์ฒด ํ˜•ํƒœ์˜ ์ฝ๊ธฐ ์ „์šฉ ๊ฐ’(ํ•จ๋ถ€๋กœ ๋ณ€๊ฒฝ๋˜์ง€ ์•Š์•„์•ผ ํ•จ)

 

๐Ÿ“ฃ  Props ์‚ฌ์šฉ๋ฒ•

1๏ธโƒฃ ํ•˜์œ„ ์ปดํฌ๋„ŒํŠธ์— ์ „๋‹ฌํ•˜๊ณ ์ž ํ•˜๋Š” ๊ฐ’๊ณผ ์†์„ฑ์„ ์ •์˜ํ•œ๋‹ค.

2๏ธโƒฃ props๋ฅผ ์ด์šฉํ•˜์—ฌ ์ •์˜๋œ ๊ฐ’๊ณผ ์†์„ฑ์„ ์ „๋‹ฌํ•œ๋‹ค.

3๏ธโƒฃ ์ „๋‹ฌ๋ฐ›์€ props๋ฅผ ๋ Œ๋”๋งํ•œ๋‹ค.

 

โžฐ <Parent>, <Child> ์ปดํฌ๋„ŒํŠธ ์„ ์–ธ

function Parent() {
  return (
    <div className="parent">
      <h1>I'm the parent</h1>
      <Child />
    </div>
  );
};

function Child() {
  return (
    <div className="child"></div>
  );
};

 

โžฐ ์†์„ฑ ๋ฐ ๊ฐ’์„ ํ• ๋‹นํ•˜๋Š” ๋ฐฉ๋ฒ•

<Child text={"I'm the eldest child"} />

 

โžฐ <Parent> ์ปดํฌ๋„ŒํŠธ์—์„œ ์ „๋‹ฌํ•œ ๋ฌธ์ž์—ด์„ <Child> ์ปดํฌ๋„ŒํŠธ์—์„œ ๋ฐ›์•„์˜ค๊ธฐ -> ํ•จ์ˆ˜ ๋งค๊ฐœ๋ณ€์ˆ˜ ์ฒ˜๋Ÿผ ์ „๋‹ฌ

โžฐ props๋Š” ๊ฐ์ฒด์ด๋ฉฐ { key: value } ์˜ ํ˜•ํƒœ๋ฅผ ๋„๊ธฐ ๋•Œ๋ฌธ์— props.text๋ฅผ ์ค‘๊ด„ํ˜ธ๋กœ ๋ฌถ์–ด์„œ ์‚ฌ์šฉ๊ฐ€๋Šฅ

function Child(props) {
  return (
    <div className="child">
    	<p>{props.text}</p>
    </div>
  );
};

 

โž• props๋ฅผ ์ „๋‹ฌํ•˜๋Š” ๋‹ค๋ฅธ ๋ฐฉ๋ฒ• => props.children

function Parent() {
  return (
    <div className="parent">
        <h1>I'm the parent</h1>
        <Child>I'm the eldest child</Child>
    </div>
  );
};

function Child(props) {
  return (
    <div className="child">
        <p>{props.children}</p>
    </div>
  );
};

โžฐ ์—ฌ๋Š” ํƒœ๊ทธ์™€ ๋‹ซ๋Š” ํƒœ๊ทธ ์‚ฌ์ด์— value๋ฅผ ๋„ฃ์–ด ์ „๋‹ฌํ•˜๋Š” ๋ฐฉ๋ฒ•

โžฐ ์ด ๊ฒฝ์šฐ props.children์œผ๋กœ ํ•ด๋‹น value์— ์ ‘๊ทผํ•˜์—ฌ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

 

๐Ÿ“ฃ  React์˜ ์ด๋ฒคํŠธ ์ฒ˜๋ฆฌ

โœ”๏ธ ์นด๋ฉœ ์ผ€์ด์Šค ์‚ฌ์šฉ

โœ”๏ธ JSX๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ•จ์ˆ˜๋กœ ์ด๋ฒคํŠธ ์ฒ˜๋ฆฌ ํ•จ์ˆ˜๋ฅผ ์ „๋‹ฌํ•œ๋‹ค.

//HTML ์ด๋ฒคํŠธ ์ฒ˜๋ฆฌ ๋ฐฉ์‹
<button onclick="handleEvent()">Event</button>

//React ์ด๋ฒคํŠธ ์ฒ˜๋ฆฌ ๋ฐฉ์‹
<button onClick={handleEvent}>Event</button>

 

1๏ธโƒฃ onChange

function NameForm() {
  const [name, setName] = useState("");

  const handleChange = (e) => {
    setName(e.target.value);
  }

  return (
    <div>
      <input type="text" value={name} onChange={handleChange}></input>
      <h1>{name}</h1>
    </div>
  )
};

โžฐ input์˜ ํ…์ŠคํŠธ๊ฐ€ ๋ฐ”๋€” ๋–„๋งˆ๋‹ค ์ด๋ฒคํŠธ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค.

โžฐ onChange ์ด๋ฒคํŠธ๊ฐ€ ๋ฐœ์ƒํ•˜๋ฉด e.target.value๋ฅผ ํ†ตํ•ด ์ด๋ฒคํŠธ ๊ฐ์ฒด์— ๋‹ด๊ฒจ์žˆ๋Š” input๊ฐ’์„ ์ฝ์–ด์˜ฌ ์ˆ˜ ์žˆ๋‹ค.

โžฐ ์ด๋ฒคํŠธ๊ฐ€ ๋ฐœ์ƒํ•˜๋ฉด hangleChangeํ•จ์ˆ˜๊ฐ€ ์ž‘๋™ํ•˜๋ฉฐ, ์ด๋ฒคํŠธ ๊ฐ์ฒด์— ๋‹ด๊ธด input๊ฐ’์„ setState๋ฅผ ํ†ตํ•ด ์ƒˆ๋กœ์šด state๋กœ ๊ฐฑ์‹ ํ•œ๋‹ค.

 

2๏ธโƒฃ onClick

function NameForm() {
  const [name, setName] = useState("");

  const handleChange = (e) => {
    setName(e.target.value);
  }

  return (
    <div>
      <input type="text" value={name} onChange={handleChange}></input>
      <button onClick={alert(name)}>Button</button>
      <h1>{name}</h1>
    </div>
  );
};

โžฐ ์œ„ ์ฝ”๋“œ๋Š” ๋ฒ„ํŠผ์„ ํด๋ฆญ ํ•  ๋•Œ๊ฐ€ ์•„๋‹Œ, ์ปดํฌ๋„ŒํŠธ๊ฐ€ ๋ Œ๋”๋ง ๋  ๋•Œ์— alert๊ฐ€ ์‹คํ–‰๋œ๋‹ค.

โžฐ ๋•Œ๋ฌธ์— onClick ์ด๋ฒคํŠธ์— ํ•จ์ˆ˜๋ฅผ ์ „๋‹ฌํ•  ๋•Œ๋Š” ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ๋ฆฌํ„ด๋ฌธ ์•ˆ์—์„œ ํ•จ์ˆ˜๋ฅผ ์ •์˜ํ•˜๊ฑฐ๋‚˜, ๋ฆฌํ„ด๋ฌธ ์™ธ๋ถ€์—์„œ ํ•จ์ˆ˜๋ฅผ ์ •์˜ํ•œ ํ›„ ์ด๋ฒคํŠธ์— ํ•จ์ˆ˜ ์ž์ฒด๋ฅผ ์ „๋‹ฌํ•ด์•ผ ํ•œ๋‹ค.

 

//ํ•จ์ˆ˜ ์ž์ฒด๋ฅผ ์ „๋‹ฌํ•˜๊ธฐ

const handleClick = () => {
  alert(name);
};

return (
  <div>
      ...
    <button onClick={handleClick}>Button</button>
      ...
  </div>
  );
};

 

๐Ÿ“ฃ  React์˜ ๋ฐ์ดํ„ฐ ํ๋ฆ„

โœ”๏ธ ํŽ˜์ด์ง€๋ฅผ ๋งŒ๋“ค๊ธฐ ์ด์ „์—, ์ปดํฌ๋„ŒํŠธ๋ฅผ ๋จผ์ € ๋งŒ๋“ค๊ณ  ์กฐ๋ฆฝํ•œ๋‹ค(์ƒํ–ฅ์‹)

โœ”๏ธ ํ•˜๋‚˜์˜ ์ปดํฌ๋„ŒํŠธ๋Š” ํ•œ ๊ฐ€์ง€ ์ผ๋งŒ ํ•œ๋‹ค.

โœ”๏ธ ๋ฐ์ดํ„ฐ๋Š” ์œ„์—์„œ ์•„๋ž˜๋กœ ํ๋ฅธ๋‹ค(ํ•˜ํ–ฅ์‹) -> ๋‹จ๋ฐฉํ–ฅ ๋ฐ์ดํ„ฐ ํ๋ฆ„

โœ”๏ธ ๋ฐ์ดํ„ฐ๋ฅผ ์ „๋‹ฌํ•˜๋Š” ์ฃผ์ฒด๋Š” ๋ถ€๋ชจ ์ปดํฌ๋„ŒํŠธ๊ฐ€ ๋œ๋‹ค.

โœ”๏ธ ๋ชจ๋“  ๋ฐ์ดํ„ฐ๋ฅผ state๋กœ ๋‘˜ ํ•„์š”๋Š” ์—†๋‹ค -> state๋Š” ์ตœ์†Œํ™”ํ•˜๋Š” ๊ฒƒ์ด ์ข‹๋‹ค.

โž• ์–ด๋–ค ๋ฐ์ดํ„ฐ๋ฅผ state๋กœ ๋‘์–ด์•ผ ํ• ๊นŒ?

๋”๋ณด๊ธฐ

1๏ธโƒฃ ๋ถ€๋ชจ๋กœ๋ถ€ํ„ฐ props๋ฅผ ํ†ตํ•ด ์ „๋‹ฌ๋ฉ๋‹ˆ๊นŒ? -> state(X)

2๏ธโƒฃ ์‹œ๊ฐ„์ด ์ง€๋‚˜๋„ ๋ณ€ํ•˜์ง€ ์•Š๋‚˜์š”? -> state(X)

3๏ธโƒฃ ์ปดํฌ๋„ŒํŠธ ์•ˆ์˜ ๋‹ค๋ฅธ state๋‚˜ props๋ฅผ ๊ฐ€์ง€๊ณ  ๊ณ„์‚ฐ ๊ฐ€๋Šฅํ•œ๊ฐ€์š”? -> state(X)

โœ”๏ธ ๋‘ ๊ฐœ์˜ ์ž์‹ ์ปดํฌ๋„ŒํŠธ๊ฐ€ ํ•˜๋‚˜์˜ ์ƒํƒœ์— ์ ‘๊ทผํ•˜๊ณ ์ž ํ•  ๋•Œ๋Š” ๋‘ ์ž์‹์˜ ๊ณตํ†ต ๋ถ€๋ชจ ์ปดํฌ๋„ŒํŠธ(๊ณตํ†ต ์†Œ์œ  ์ปดํฌ๋„ŒํŠธ)์— state๋ฅผ ์œ„์น˜์‹œ์ผœ์•ผ ํ•œ๋‹ค.