React ๋ง›๋ณด๊ธฐ ๐Ÿ˜‹

์ง„ยท2023๋…„ 11์›” 21์ผ
0

FE

๋ชฉ๋ก ๋ณด๊ธฐ
1/3

๋ชฉํ‘œ

(0) ํ™˜๊ฒฝ ์„ค์ • ๋ฐ base code ์งœ๊ธฐ

(1) hook ์‚ฌ์šฉํ•ด๋ณด๊ธฐ(useState)

(2) components ์‚ฌ์ด์— data sharingํ•˜๋Š” ์ž‘์—…

์„ ํ•ด๋ณธ๋‹ค.

๊ฐ (0), (1), (2) ๋ชฉํ‘œ๋Š” ๊ฐ ์‹ค์Šต์— ๋”ฐ๋ผ ์ด๋ค„์ง„๋‹ค.

React ํ™˜๊ฒฝ ์„ค์ •ํ•˜๊ธฐ

Nodejs ์„ค์น˜ํ•˜๊ธฐ & npm ์„ค์น˜ํ•˜๊ธฐ & npx ์„ค์น˜ํ•˜๊ธฐ

node.js ๋‹ค์šด๋กœ๋“œ

https://nodejs.org/en ์—์„œ LTS ๋ฒ„์ „ ๋‹ค์šด๋กœ๋“œ ํ›„ ์„ค์น˜(๋””ํดํŠธ ์„ค์ •)

cmd ์ฐฝ์—์„œ node -v ์ž…๋ ฅ ํ›„ ๋ฒ„์ „ ํ™•์ธ

cmd ์ฐฝ์—์„œ npm -v ์ž…๋ ฅ ํ›„ ๋ฒ„์ „ ํ™•์ธ

npx install

cmd ์ฐฝ์—์„œ ์•„๋ž˜ ์ž…๋ ฅ

npm install -g create-react-app

react ํ”„๋กœ์ ํŠธ ๋งŒ๋“ค๊ธฐ

npx create-react-app yum

or playcode.io/react ์—์„œ ์ˆ˜์ •ํ•˜๊ธฐ

BASE CODE ๋ณต๋ถ™

App.js

import logo from "./logo.svg";
import "./App.css";
import React, { useState } from "react";

function Counter() {
  const counter = 0;
  return (
    <div className="Counter-Wrapper">
      <div>COUNTER: {counter}</div>
      <button>+1</button>
      <button>-1</button>
    </div>
  );
}

function App() {
  return (
    <div className="App">
      <Counter />
    </div>
  );
}

export default App;

App.css

.App {
  text-align: center;
  background-color: #282c34;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  font-size: calc(10px + 2vmin);
  color: white;
}

.Counter-Wrapper {
  margin-bottom: 15px;
}

.Counter-Wrapper > div {
  border: 1px solid white;
  padding: 5px;
  border-radius: 5px;
}

.Counter-Wrapper > button {
  margin-top: 15px;
  margin-left: 10px;
  min-height: calc(10px + 3vmin);
  min-width: calc(10px + 5vmin);
  font-size: calc(5px + 2vmin);
}

(1) hook ์‚ฌ์šฉํ•ด๋ณด๊ธฐ(useState, useEffect)

COUNTER๊ฐ€ ์ฆ๊ฐ€ํ•˜๊ณ , ๊ฐ์†Œํ•˜๋„๋ก ํ•จ์ˆ˜ Counter๋ฅผ ์ž‘์„ฑํ•ด ์ค€๋‹ค.

React ์ด๋ก  ์„ค๋ช…


โฒ๏ธ ๋ฆฌ์•กํŠธ์—์„œ ๋ฐ์ดํ„ฐ๊ฐ€ ๋ณ€ํ•ด ์›น ๋ธŒ๋ผ์šฐ์ €์—์„œ ์‹ค์ œ DOM์— ์—…๋ฐ์ดํŠธํ•  ๋•Œ ๋ฐŸ๋Š” 3๊ฐ€์ง€ ์ ˆ์ฐจ โฒ๏ธ

  1. ๋ฐ์ดํ„ฐ๋ฅผ ์—…๋ฐ์ดํŠธํ•˜๋ฉด ์ „์ฒด UI์—์„œ Virtual DOM์— re-render
  2. ์ด์ „ Virtual DOM์— ์žˆ๋˜ ๋‚ด์šฉ๊ณผ ํ˜„์žฌ ๋‚ด์šฉ์„ ๋น„๊ต
  3. ๋ฐ”๋€ ๋ถ€๋ถ„๋งŒ ์‹ค์ œ DOM์— ์ ์šฉํ•จ

๊ทธ๋ž˜์„œ ์•„๊นŒ [๊ทธ๋ฆผ 1-6]์˜ ๋ฐ”๋€ DOM ํŠธ๋ฆฌ๊ฐ€ ๊ทธ VIrtual DOM ํŠธ๋ฆฌ๋กœ ํ™•์ธํ•˜๋Š” ๊ฒƒ์ž„.

  • useState: state์™€ state์„ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ๋Š” ํ•จ์ˆ˜๋ฅผ ์ œ๊ณตํ•ด์ค€๋‹ค. state๊ฐ€ ์žˆ์–ด์•ผ ์ปดํฌ๋„ŒํŠธ์— ๋ณ€๊ฒฝ๋œ ๊ฐ’์ด ๊ฐ์ง€๋˜๊ณ , ๋ฆฌ๋ Œ๋”๋จ(reconciliation ๊ณผ์ •).
    • ์˜ˆ์‹œ ์ฝ”๋“œ
      // ์˜ˆ์‹œ
      import React, { useState } from "react";
      
      function App() {
      	const [counter, setCounter] = useState(0);
        return (
          <div className="App">
      			{counter}
          </div>
        );
      }
  • onClick: button ๊ณผ ๊ฐ™์€ ํŽ˜์ด์ง€ ์š”์†Œ์—์„œ event ๋ฐœ์ƒ ์‹œ ํ˜ธ์ถœ๋˜๋Š” ํ•จ์ˆ˜ ์ค‘ ํ•˜๋‚˜. ํ•œ๋งˆ๋””๋กœ, button ํด๋ฆญ ์‹œ ํ˜ธ์ถœ๋˜๋Š” ์ด๋ฒคํŠธ ํ•จ์ˆ˜๋‹ค.
    • ์˜ˆ์‹œ ์ฝ”๋“œ
      <button onClick={()=>{console.log("+1 clicked")}>+1</button>
      // ์—ฌ๊ธฐ์„œ ()=>{}๋Š” ํ™”์‚ดํ‘œ ํ•จ์ˆ˜(JS ๋ฌธ๋ฒ•)๋‹ค. onClick ์ด๋ฒคํŠธ ๋ฐœ์ƒ ์‹œ ํ˜ธ์ถœ๋˜๋Š” ํ•จ์ˆ˜.

(2) components ์‚ฌ์ด์— data sharingํ•˜๋Š” ์ž‘์—…์„ ํ•ด๋ณธ๋‹ค.

์œ„ ์‚ฌ์ง„๊ณผ ๊ฐ™์ด +1 ํ˜น์€ -1 ๋ฒ„ํŠผ ํด๋ฆญ ์‹œ, ํ•ด๋‹น ๊ฒฐ๊ณผ๊ฐ’๋งŒํผ ํŠน์ • ๋ฌธ์ž์—ด์ด ๋ฐ˜๋ณต๋˜๋„๋ก ํ•œ๋‹ค.

  • ์ž์‹ ์ปดํฌ๋„ŒํŠธ์—์„œ ๋ฐ์ดํ„ฐ ๋ณ€๊ฒฝ ์ž‘์—…์„ ํ•ด๋„ ๋‹ค๋ฅธ ์ž์‹ ์ปดํฌ๋„ŒํŠธ๊ฐ€ ํ•ด๋‹น ์‚ฌ์‹ค์„ ์•Œ๋„๋ก ํ•ด์•ผ ํ•œ๋‹ค.
  • props ์ „๋‹ฌ: ๋ถ€๋ชจ ์ปดํฌ๋„ŒํŠธ์—์„œ ์ž์‹ ์ปดํฌ๋„ŒํŠธ๋กœ ์ „๋‹ฌํ•  ๋•Œ ์ธ์ž๋กœ ์ „๋‹ฌํ•œ๋‹ค. ์ด๋•Œ ์ธ์ž๋ฅผ ์˜๋ฏธ. ๊ทธ๋Ÿฐ๋ฐ ์ธ์ž๋กœ ํ•จ์ˆ˜๋„ ์ „๋‹ฌํ•  ์ˆ˜ ์žˆ๋‹ค!
    • ์˜ˆ์‹œ ์ฝ”๋“œ
      function Counter({ counter, setCounter }) {
      	// ...
      }
      
      // ํ˜น์€
      function Counter(props) {
      	const counter = props.counter;
      	const setCounter = props.setCounter;
      	// ...
      }
      
      function App() {
        const [counter, setCounter] = useState(0);
        return (
          <div className="App">
            <Counter counter={counter} setCounter={setCounter} />
          </div>
        );
      }

React ์„ค๋ช…

Often youโ€™ll need components to share data and always update together.

To make both MyButton components display the same count and update together, you need to move the state from the individual buttons โ€œupwardsโ€ to the closest component containing all of them.

profile
์ด๊ฒƒ์ €๊ฒƒ ๊ฐœ๋ฐœํ•˜๋Š” ๊ฒƒ ์ข‹์•„ํ•˜์ง€๋งŒ ์„œ๋ฒ„ ๊ฐœ๋ฐœ์ด ์ œ์ผ ์ข‹๋”๋ผ๊ตฌ์š”..

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