[TIL] Day29 #Props #State

Beanxxยท2022๋…„ 6์›” 7์ผ
0

TIL

๋ชฉ๋ก ๋ณด๊ธฐ
29/120
post-thumbnail

[TIL] Day29
[SEB FE] Day29

โ˜‘๏ธย React State & Props

๐Ÿ“Žย Props

  • ์ปดํฌ๋„ŒํŠธ์˜ ์†์„ฑ(property)
  • ์™ธ๋ถ€๋กœ๋ถ€ํ„ฐ ์ „๋‹ฌ๋ฐ›์€ ๊ฐ’ [๋ถ€๋ชจ ์ปดํฌ๋„ŒํŠธ(์ƒ์œ„ ์ปดํฌ๋„ŒํŠธ)๋กœ๋ถ€ํ„ฐ ์ „๋‹ฌ๋ฐ›์€ ๊ฐ’]
    ๐Ÿ‘‰ ์ปดํฌ๋„ŒํŠธ๊ฐ€ ์ตœ์ดˆ ๋ Œ๋”๋ง ๋  ๋•Œ ํ™”๋ฉด์— ์ถœ๋ ฅํ•˜๊ณ ์ž ํ•˜๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ๋‹ด์€ ์ดˆ๊นƒ๊ฐ’์œผ๋กœ ์‚ฌ์šฉ ๊ฐ€๋Šฅ
  • ๊ฐ์ฒด ํ˜•ํƒœ
  • ์ฝ๊ธฐ ์ „์šฉ; ready-only (๋ณ€๊ฒฝ โŒ) โ‡’ immutableํ•œ ๋ฐ์ดํ„ฐ
  • ๋ฐ์ดํ„ฐ๋ฅผ ์ „๋‹ฌํ•˜๋Š” ๋ฐ ์‚ฌ์šฉํ•จ
  • ํด๋ž˜์Šค ์ปดํฌ๋„ŒํŠธ, ํ•จ์ˆ˜ํ˜• ์ปดํฌ๋„ŒํŠธ ๋ชจ๋‘์—์„œ ์‚ฌ์šฉ ๊ฐ€๋Šฅ
  • ์—ฌ๋Ÿฌ ๊ฐœ ์ง€์ • ๊ฐ€๋Šฅ

โœ”๏ธย Props ์‚ฌ์šฉ ๋ฐฉ๋ฒ•

  1. ํ•˜์œ„ ์ปดํฌ๋„ŒํŠธ์— ์ „๋‹ฌํ•˜๊ณ ์ž ํ•˜๋Š” ๊ฐ’&์†์„ฑ ์ •์˜
  2. props๋ฅผ ์ด์šฉํ•˜์—ฌ ์ •์˜๋œ ๊ฐ’&์†์„ฑ ์ „๋‹ฌ
  3. ์ „๋‹ฌ๋ฐ›์€ props ๋ Œ๋”๋ง
function Parent() {
  return (
    <div className="parent">
      <h1>I'm the parent</h1>
      <Child text={"I'm the eldest child"}/> {/* ์†์„ฑ & ๊ฐ’ ํ• ๋‹น */}
    </div>
  );
};

function Child(props) {
  return (
    <div className="child">
			<p>{props.text}</p> {/* props ๋ Œ๋”๋ง */}
		</div>
  );
};

โœ‹ย props๋Š” {attribute: value} ํ˜•ํƒœ๋กœ ์ถœ๋ ฅ


โœ”๏ธย props.children

ํƒœ๊ทธ ์‚ฌ์ด์— value๋ฅผ ๋„ฃ์–ด ์ „๋‹ฌ โ†’ ํ•ด๋‹น value์— ์ ‘๊ทผํ•˜์—ฌ ์‚ฌ์šฉ ๊ฐ€๋Šฅ

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>
  );
};

๐Ÿ“Žย State

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

โœ”๏ธย useState

state๋ฅผ ๋‹ค๋ฃจ๋Š” ๋ฐฉ๋ฒ• ์ค‘ ํ•˜๋‚˜

โžฐ useState ํ˜ธ์ถœ

const [state ์ €์žฅ ๋ณ€์ˆ˜, state ๊ฐฑ์‹  ํ•จ์ˆ˜] = useState(์ƒํƒœ ์ดˆ๊ธฐ ๊ฐ’);

import { useState} from 'react';

function CheckboxExample() {
	// useState ํ˜ธ์ถœ = 'state' ๋ณ€์ˆ˜ ์„ ์–ธ (ํ•จ์ˆ˜๊ฐ€ ๋๋‚˜๋„ ์‚ฌ๋ผ์ง€์ง€ ์•Š์Œ)
	const [isChecked, setIsChecked] = useState(false);
	// setIsChecked: state 'isChecked'๋ฅผ ๋ณ€๊ฒฝํ•˜๋Š” ํ•จ์ˆ˜
}

// ์‚ฌ์šฉ
<span>{isChecked ? "Checked!!" : "Unchecked"}</span>
โžฐ state ๊ฐฑ์‹ 
function CheckboxExample() {
  const [isChecked, setIsChecked] = useState(false);

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

  return (
    <div className="App">
      <input type="checkbox" checked={isChecked} onChange={handleChecked} />
      <span>{isChecked ? "Checked!!" : "Unchecked"}</span>
    </div>
  );
}

โœ‹ย ์ปดํฌ๋„ŒํŠธ์˜ ์ƒํƒœ(state)๊ฐ€ ๋ณ€๊ฒฝ๋  ๋•Œ๋งˆ๋‹ค ์ƒˆ๋กญ๊ฒŒ ํ˜ธ์ถœ๋˜๊ณ , ๋ฆฌ๋ Œ๋”๋ง๋จ

โœ‹ย React state๋Š” ์ƒํƒœ ๋ณ€๊ฒฝ ํ•จ์ˆ˜ ํ˜ธ์ถœ๋กœ ๋ณ€๊ฒฝํ•ด์•ผ ํ•จ (๊ฐ•์ œ ๋ณ€๊ฒฝ ์‹œ๋„ โŒ)

โœ‹ย state๋Š” ํ•˜์œ„ ์ปดํฌ๋„ŒํŠธ์—์„œ๋„ ์กด์žฌ ๊ฐ€๋Šฅ


๐Ÿ“Žย Event handling

  • camelCase ์‚ฌ์šฉ
  • JSX ์‚ฌ์šฉํ•˜์—ฌ ๋ฌธ์ž์—ด์ด ์•„๋‹Œ ํ•จ์ˆ˜๋กœ ์ด๋ฒคํŠธ ์ฒ˜๋ฆฌ ํ•จ์ˆ˜(Event handler) ์ „๋‹ฌ
<button onClick={handleEvent}>Event</button>

โœ”๏ธ onChange

  • React์—์„  ๋ณ€๊ฒฝ๋  ์ˆ˜ ์žˆ๋Š” ์ž…๋ ฅ๊ฐ’์„ ์ปดํฌ๋„ŒํŠธ์˜ state๋กœ ๊ด€๋ฆฌํ•˜๊ณ  ์—…๋ฐ์ดํŠธํ•จ
โžฐ onChange example Logic
  • onChange ์ด๋ฒคํŠธ ๋ฐœ์ƒ โ†’ e.target.value๋ฅผ ํ†ตํ•ด ์ด๋ฒคํŠธ ๊ฐ์ฒด์— ๋‹ด๊ฒจ์žˆ๋Š” input๊ฐ’์„ ์ฝ์–ด์˜ด
  • ์ด๋ฒคํŠธ ๋ฐœ์ƒ โ†’ handleChange ํ•จ์ˆ˜ ์ž‘๋™ โ†’ ์ด๋ฒคํŠธ ๊ฐ์ฒด์— ๋‹ด๊ธด input ๊ฐ’์„ setState๋ฅผ ํ†ตํ•ด ์ƒˆ๋กœ์šด state๋กœ ๊ฐฑ์‹ 

โœ”๏ธย onClick

function NameForm() {
	// useState ํ˜ธ์ถœ
	const [name, setName] = useStaet("");
	
	// event handling
	const handleChange = (e) => {
		setName(e.target.value);
	}

	return (
		<div>
			<input type="text" value={name} onChange={handleChange}></input>
			<button onClick={alert(name)}>Button</button>
			{/* alert(name) ํ•จ์ˆ˜๋ฅผ ๋ฐ”๋กœ ํ˜ธ์ถœ
			 -> ์ปดํฌ๋„ŒํŠธ ๋ Œ๋”๋ง๋  ๋•Œ ํ•จ์ˆ˜ ์ž์ฒด๊ฐ€ ์•„๋‹Œ ํ•จ์ˆ˜ ํ˜ธ์ถœ ๊ฒฐ๊ณผ๊ฐ€ onClick์— ์ ์šฉ
			 -> (๋ฒ„ํŠผ ํด๋ฆญํ•  ๋•Œ๊ฐ€ ์•„๋‹Œ) ๋ Œ๋”๋ง๋  ๋•Œ ์‹คํ–‰๋˜์–ด์„œ undefined ๋ฐ˜ํ™˜
       		 => onClick ์ด๋ฒคํŠธ์— ํ•จ์ˆ˜ ์ „๋‹ฌ์‹œ ๋ฆฌํ„ด๋ฌธ ์•ˆ์—์„œ ํ•จ์ˆ˜ ์ •์˜ํ•˜๊ฑฐ๋‚˜ 
					๋ฆฌํ„ด๋ฌธ ์™ธ๋ถ€์—์„œ ํ•จ์ˆ˜ ์ •์˜ ํ›„ ์ด๋ฒคํŠธ์— ํ•จ์ˆ˜ ์ž์ฒด๋ฅผ ์ „๋‹ฌํ•ด์•ผ ํ•จ */}
		</div>
	)
};
// ํ•จ์ˆ˜ ์ •์˜
return (
	<div>
		<button onClick={() => alert(name)}Button</button>
	</div>
	)
};

----------

// ํ•จ์ˆ˜ ์ž์ฒด ์ „๋‹ฌ
const handleClick = () => {
	alert(name);
};

return (
	<div>
		<button onClick={handleClick}>Button</button>
	</div>
	);
};
// pop-up example

// ๊ธฐ๋ณธ ํ™”๋ฉด์œผ๋กœ 'Open me' Button ํ‘œ์‹œ -> ๋ˆ„๋ฅด๋ฉด 'Success!' ๋ฌธ๊ตฌ์™€ ํ•จ๊ป˜
// 'Close me' Button์ด ํŒ์—…์œผ๋กœ ๋“ฑ์žฅ
// 'Close me' Button ๋ˆ„๋ฅด๋ฉด ๋‹ค์‹œ ํŒ์—… ๋‹ซํž˜

import React, { useState } from "react";
import "./styles.css";

function App() {
  const [showPopup, setShowPopup] = useState(false);

  const togglePopup = () => {
    setShowPopup(!showPopup)
		
	 // ! ๋‚œ ์•„๋ž˜์ฒ˜๋Ÿผ ์กฐ๊ฑด๋ฌธ์„ ์ด์šฉํ•ด์„œ ํ–ˆ๋Š”๋ฐ ์œ„์ฒ˜๋Ÿผ ํ•˜๋ฉด ํ•œ์ค„์— ์™„์ „ ๊ฐ„๋‹จํžˆ ๊ตฌํ˜„ ๊ฐ€๋Šฅ !
	 // if(showPopup === false) {
	 // setShowPopup(true)
	 // } else {
	 //   setShowPopup(false)
	 // }
  };

  return (
    <div className="App">
      <h1>Fix me to open Pop Up</h1>
      <button className="open" onClick={togglePopup}>
        Open me
      </button>
      {showPopup ? (
        <div className="popup">
          <div className="popup_inner">
            <h2>Success!</h2>
            <button className="close" onClick={togglePopup}>
              Close me
            </button>
          </div>
        </div>
      ) : null}
    </div>
  );
}

export default App;

๐Ÿ“Žย Controlled Component

// controlled-component example
import "./styles.css";
import React, { useState } from "react";

export default function App() {
  const [username, setUsername] = useState("");
  const [msg, setMsg] = useState("");

  const handleChangeMsg = (event) => {
    setMsg(event.target.value);
  };

  return (
    <div className="App">
      <div>{username}</div>
      <input
        type="text"
        value={username}
        onChange={(event) => setUsername(event.target.value)}
        placeholder="์—ฌ๊ธฐ๋Š” ์ธํ’‹์ž…๋‹ˆ๋‹ค."
        className="tweetForm__input--username"
      ></input>
      <div>{msg}</div>

      <textarea
        placeholder="์—ฌ๊ธฐ๋Š” ํ…์ŠคํŠธ ์˜์—ญ์ž…๋‹ˆ๋‹ค."
        className="tweetForm__input--message"
        onChange={handleChangeMsg}
        value={msg}
      ></textarea>
    </div>
  );
}

๐Ÿ“Žย React ๋ฐ์ดํ„ฐ ํ๋ฆ„

  • React ๊ฐœ๋ฐœ ๋ฐฉ์‹์€ ํŽ˜์ด์ง€ ๋‹จ์œ„๊ฐ€ ์•„๋‹Œ, ์ปดํฌ๋„ŒํŠธ ๋‹จ์œ„๋กœ ์‹œ์ž‘

  • ์ƒํ–ฅ์‹(bottom-up)์œผ๋กœ ์•ฑ ๊ฐœ๋ฐœ โ†’ ํ…Œ์ŠคํŠธ๊ฐ€ ์‰ฝ๊ณ  ํ™•์žฅ์„ฑ ๐Ÿ‘

  • React๋Š” ๋‹จ๋ฐฉํ–ฅ ๋ฐ์ดํ„ฐ ํ๋ฆ„(one-way data flow)์„ ๋”ฐ๋ฆ„

  • ์ปดํฌ๋„ŒํŠธ ๊ณ„์ธต ๊ตฌ์กฐ๋กœ ๋‚˜๋ˆ„๋Š” ๊ฒƒ์ด ๊ฐ€์žฅ ๋จผ์ € ํ•ด์•ผํ•  ์ผ!

    ๐Ÿ‘‰ย ํŽ˜์ด์ง€๋ฅผ ๋งŒ๋“ค๊ธฐ ์ด์ „์—, ์ปดํฌ๋„ŒํŠธ๋ฅผ ๋จผ์ € ๋งŒ๋“ค๊ณ  ์กฐ๋ฆฝํ•˜๊ธฐ

ย โœ‹ย ๋ฐ์ดํ„ฐ ์ „๋‹ฌ ์ฃผ์ฒด๋Š” ๋ถ€๋ชจ ์ปดํฌ๋„ŒํŠธ
๋ฐ์ดํ„ฐ ํ๋ฆ„-ํ•˜ํ–ฅ์‹(top-down)) โ‡’ ๋ฐ์ดํ„ฐ๋Š” ์œ„์—์„œ ์•„๋ž˜๋กœ!

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

๐Ÿคทโ€โ™€๏ธย Q: ์–ด๋–ค ๋ฐ์ดํ„ฐ๋ฅผ state๋กœ ๋‘์–ด์•ผ ํ•˜๋‚˜์š”?

๐Ÿ™†โ€โ™€๏ธย A: 1. ๋ถ€๋ชจ๋กœ๋ถ€ํ„ฐ props๋ฅผ ํ†ตํ•ด ์ „๋‹ฌ๋˜๋ฉด โŒ
ย ย ย ย ย ย ย ย ย  2. ์‹œ๊ฐ„์ด ์ง€๋‚˜๋„ ๋ณ€ํ•˜์ง€ ์•Š๋Š”๋‹ค๋ฉด โŒ
ย ย ย ย ย ย ย ย ย  3. ์ปดํฌ๋„ŒํŠธ ์•ˆ์˜ ๋‹ค๋ฅธ state๋‚˜ props๋กœ ๊ณ„์‚ฐ์ด ๊ฐ€๋Šฅํ•˜๋‹ค๋ฉด โŒ



โ˜‘๏ธย [Pair] React Twittler State & Props

3์ผ๊ฐ„์˜ ํœด์ผ์ด ๋๋‚˜๊ณ , ๋ถˆ๊ณผ ๋ช‡์ผ ์ „์— ์ง„ํ–‰ํ•œ Intro, SPA ์ฝ”๋“œ๋„ ๊ธฐ์–ต์ด ์•ˆ ๋‚˜์„œ ๋”๋“ฌ๋”๋“ฌ ์ „ ์ฝ”๋“œ๋ฅผ ๊ตฌํ˜„ํ–ˆ๋‹ค.
+ํŠธ์œ— ์ „์†ก Form ๊ธฐ๋Šฅ์„ ์ถ”๊ฐ€ ๊ตฌํ˜„ํ–ˆ์–ด์•ผ ํ–ˆ๋Š”๋ฐ ์‰ฝ์ง€ ์•Š์•˜๋‹ค.๐Ÿฅฒ
์•„ ๊ทธ๋ฆฌ๊ณ  ๊ณ„์† console ์ฐฝ์— Warning: Each child in a list should have a unique โ€œkeyโ€ prop. ๊ฒฝ๊ณ ์ฐฝ์ด ๊ณ„์† ๋– ์žˆ์–ด์„œ ์—†์• ๋Š๋ผ ์• ๋จน์—ˆ๋Š”๋ฐ ํŽ˜์–ด๋ถ„์ด map์žˆ๋Š” ๊ณณ์— key๋ฅผ ์ถ”๊ฐ€ํ•ด์„œ ์—†์•ด๋‹ค. map & key ํ•œ ์Œ์œผ๋กœ ๊ธฐ์–ตํ•˜์ž..
ํŽ˜์–ด๋ถ„๊ณผ ํ•จ๊ป˜ํ•ด์„œ ์–ด์ฐŒ์–ด์ฐŒ Bare Minimum Requirement Test๋Š” ํ†ต๊ณผํ–ˆ์ง€๋งŒ,
์•„์ง๋„ ๋ญ”๊ฐ€ ์ฝ”๋“œ๊ฐ€ ๋จธ๋ฆฟ์†์—์„œ ์—‰ํ‚จ ๋Š๋‚Œ์ด๋ผ ์ข€ ๋” ๊ณต๋ถ€ํ•˜๊ณ  ์ฝ”๋“œ ์ดํ•ด ๊ณผ์ •?์„ ์ •๋ฆฌํ•ด๋ด์•ผ๊ฒ ๋‹ค.
์˜ค๋Š˜์€ ์ˆ˜์—… ๋๋‚˜๊ณ  ์•Œ๋ฐ” ๋Œ€ํƒ€ ๊ฐ”๋‹ค์™€์„œ ์ถ”๊ฐ€ ํ•™์Šต์„ ํ•  ์‹œ๊ฐ„์ด ์—†์—ˆ๋‹ค.. (ํ•‘๊ณ„๐Ÿซ )
๋‚ด์ผ์€ Advanced Challenge์ธ ํ•„ํ„ฐ ๊ธฐ๋Šฅ๊ณผ ํŠธ์œ— ์‚ญ์ œ ๊ธฐ๋Šฅ ๊ตฌํ˜„ํ•ด๋ณด๊ธฐ!

แ„‰แ…ณแ„แ…ณแ„…แ…ตแ†ซแ„‰แ…ฃแ†บ 2022-06-08 แ„‹แ…ฉแ„Œแ…ฅแ†ซ 2 17 19 แ„‰แ…ณแ„แ…ณแ„…แ…ตแ†ซแ„‰แ…ฃแ†บ 2022-06-08 แ„‹แ…ฉแ„Œแ…ฅแ†ซ 2 25 30
profile
FE developer

0๊ฐœ์˜ ๋Œ“๊ธ€