230516 - useState

๋ฐฑ์Šน์—ฐยท2023๋…„ 5์›” 17์ผ
1

๐Ÿšฉ React

useState(์ค‘์ฒฉ๋œ ๊ฐ์ฒด)

  • prompt๋กœ ๊ฐ’์„ ๋ฐ›์•„์„œ ์ด์ „์— ์žˆ๋˜ ๊ฐ’ ๊ต์ฒด

โœ’๏ธ ์ฝ”๋“œ ์ž‘์„ฑ

์ž…๋ ฅ

App.js

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

export default function App() {
  const [person, setPerson] = useState({
    // person์˜ ์ดˆ๊ธฐ๊ฐ’
    name: "์ „์ง€ํ˜„",
    work: "๊ฐœ๋ฐœ์ž",
    mentor: {
      name: "๋งˆ์ดํด",
      work: "์‹œ๋‹ˆ์–ด๊ฐœ๋ฐœ์ž"
    }
  });

  return (
    <div className="App">
      <h1>
        {person.name}์€ {person.work}์ž…๋‹ˆ๋‹ค.
      </h1>
      <h4>
        {person.name}์˜ ๋ฉ˜ํ† ๋Š” {person.mentor.name} ({person.mentor.work}
        )์ž…๋‹ˆ๋‹ค.
      </h4>

      <button
        onClick={() => {
          const aa = prompt("์ƒˆ๋กœ์šด ๋ฉ˜ํ† ์˜ ์ด๋ฆ„์€?");
          setPerson((person) => ({
            ...person,
            mentor: {
              ...person.mentor,
              name: aa // ๋ณ€์ˆ˜ aa๊ฐ€ name์ผ ๋•Œ name์œผ๋กœ ์ถ•์•ฝ ๊ฐ€๋Šฅ
            }
          }));
        }}
      >
        ๋ฉ˜ํ†  ์ด๋ฆ„ ๋ฐ”๊พธ๊ธฐ
      </button>
      <button
        onClick={() => {
          const work = prompt("์ƒˆ๋กœ์šด ๋ฉ˜ํ† ์˜ ์ง์—…์€?");
          // ๊ธฐ์กด์— ์žˆ๋Š” ๊ฐ’ ์ „๋ถ€ ์ถœ๋ ฅ
          // ...person.mentor : person์•ˆ์— ์žˆ๋Š” metnor๋“ค์„ ์ „๋ถ€ ๊ฐ€์ ธ์˜ด
          setPerson((person) => ({
            ...person,
            mentor: {
              ...person.mentor,
              work
            }
          }));
        }}
      >
        ๋ฉ˜ํ†  ์ง์—… ๋ฐ”๊พธ๊ธฐ
      </button>
    </div>
  );
}



์ถœ๋ ฅ

  • ์ด๋ฏธ์ง€๋กœ ๋Œ€์ฒด




useState(๋ฐฐ์—ด ๊ฐ์ฒด)

  • prompt๋กœ ๊ฐ’์„ ๋ฐ›์•„์„œ ์ด์ „์— ์žˆ๋˜ ๊ฐ’ ๊ต์ฒด
  • ์œ„ ์˜ˆ์ œ์—์„œ ์‘์šฉ

โœ’๏ธ ์ฝ”๋“œ ์ž‘์„ฑ

์ž…๋ ฅ

App.js

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

export default function App() {
  const initialState = {
    // person์˜ ์ดˆ๊ธฐ๊ฐ’
    name: "์ „์ง€ํ˜„",
    work: "๊ฐœ๋ฐœ์ž",
    mentors: [
      {
        name: "๋งˆ์ดํด",
        work: "์‹œ๋‹ˆ์–ด๊ฐœ๋ฐœ์ž"
      },
      {
        name: "ใ…‡ใ……ใ…‡",
        work: "ํšŒ์žฅ"
      },
      {
        name: "๊ผฌ๋งˆ",
        work: "๊ฐ•์•„์ง€"
      }
    ]
  };

  // ๋ฉ˜ํ†  ์ด๋ฆ„ ๋ฐ”๊พธ๊ธฐ ํ•จ์ˆ˜ ์ •์˜
  const updateMento = () => {
    const prev = prompt("๋ฐ”๊พธ๊ณ  ์‹ถ์€ ์ด๋ฆ„์€?");
    const current = prompt("์ƒˆ๋กœ ์ง€์ •ํ•  ์ด๋ฆ„์€?");
    setPerson((person) => ({
      ...person, // person ์ „์ฒด ๋‚˜์—ด(name, work, mentos)
      /*
          ์˜ค๋ธŒ์ ํŠธ ๋‹จ์œ„๋กœ map ๋Œ๋ฆผ
          person.mentors ๋ผ๋Š” array๋ฅผ map์„ ์‚ฌ์šฉํ•˜์—ฌ item{} ๊ธฐ์ค€์œผ๋กœ ํ•˜๋‚˜์”ฉ ๊ฒ€์‚ฌ
        */
      mentors: person.mentors.map((item) => {
        // prev๊ฐ’์ด ๊ฐ™์€ ๊ฒƒ์ด ์žˆ๋Š”์ง€ ํ™•์ธ. item์˜ ์ด๋ฆ„์ด prev์™€ ๊ฐ™์œผ๋ฉด,
        if (item.name === prev) {
          // name์„ ๋‚˜์ค‘์— ์ž…๋ ฅํ•œ ๊ฐ’(current)๋กœ ๋ฐ”๊ฟ”์คŒ
          return { ...item, name: current };
        }
        return item;
      })
    }));
  };

  // ๋ฉ˜ํ†  ์‚ญ์ œ ํ•จ์ˆ˜ ์ •์˜
  const deleteMento = () => {
    const del = prompt("์‚ญ์ œํ•˜๊ณ  ์‹ถ์€ ์ด๋ฆ„์€?");

    setPerson(() => ({
      ...person,
      // array๋ฅผ ์‚ญ์ œํ•ด์•ผ ํ•˜๋ฏ€๋กœ map ๋Œ€์‹  filter ์‚ฌ์šฉ (* filter๋Š” ๋ฆฌํ„ด๊ฐ’์ด array)
      mentors: person.mentors.filter((item) => item.name !== del)
    }));
  };

  // ๋ฉ˜ํ†  ์ถ”๊ฐ€ ํ•จ์ˆ˜ ์ •์˜
  const addMento = () => {
    const name = prompt("์ถ”๊ฐ€ํ•  ๋ฉ˜ํ† ์˜ ์ด๋ฆ„์€?");
    const work = prompt("์ถ”๊ฐ€ํ•  ๋ฉ˜ํ† ์˜ ์ง์—…์€?");

    setPerson(() => ({
      ...person,
      mentors: [person.mentors, { name, work }]
    }));
  };
  const [person, setPerson] = useState(initialState);

  return (
    <div className="App">
      <h1>
        {person.name}์€ {person.work}์ž…๋‹ˆ๋‹ค.
      </h1>
      <h4>{person.name}์˜ ๋ฉ˜ํ† ๋Š” : </h4>

      {/* ์„ธ ๋ฒˆ์งธ ๋ฉ˜ํ†  - {person.mentors[2].name} */}
      <ul>
        {person.mentors.map((item, index) => (
          <li key={index}>
            {item.name} ({item.work})
          </li>
        ))}
      </ul>

      <button onClick={updateMento}>๋ฉ˜ํ†  ์ด๋ฆ„ ๋ฐ”๊พธ๊ธฐ</button>
      <button onClick={deleteMento}>๋ฉ˜ํ†  ์‚ญ์ œ</button>
      <button onClick={addMento}>๋ฉ˜ํ†  ์ถ”๊ฐ€</button>
    </div>
  );
}



์ถœ๋ ฅ

  • ์ด๋ฏธ์ง€๋กœ ๋Œ€์ฒด

์ฒซ ํ™”๋ฉด

์ด๋ฆ„ ๋ณ€๊ฒฝ


์‚ญ์ œ

์ถ”๊ฐ€







profile
๊ณต๋ถ€ํ•˜๋Š” ๋ฒจ๋กœ๊ทธ

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