๐Ÿ” ํ”„๋กœ์ ํŠธ1. ์นด์šดํ„ฐ ์•ฑ

๊น€์ˆ˜์ง„ยท2025๋…„ 7์›” 8์ผ

ํ•œ ์ž… ํฌ๊ธฐ๋กœ ์ž˜๋ผ ๋จน๋Š” ๋ฆฌ์•กํŠธ ์„น์…˜ 7- ํ”„๋กœ์ ํŠธ1. ์นด์šดํ„ฐ ์•ฑ

ํ”„๋กœ์ ํŠธ ์ค€๋น„ํ•˜๊ธฐ

npm create vite@latest
npm i

<App/>
<Viewer/>
<Controller/>

๋กœ ๋‚˜๋ˆ„์–ด ์นด์šดํ„ฐ ์•ฑ์„ ๋งŒ๋“ ๋‹ค.

.eslintrc.cjs ์—์„œ no-unused-vars": "off", "react/prop-types": "off" ์˜ต์…˜์„ ์ž‘์„ฑํ•œ๋‹ค.

    rules: {
      "no-unused-vars": "off",
      "react/prop-types": "off",
    },
  },
]);

public ํด๋” ์•„๋ž˜์— ์žˆ๋Š” vite.svg ํŒŒ์ผ ์‚ญ์ œ & assets ํด๋” ์•„๋ž˜์— ์žˆ๋Š” react.svg ํŒŒ์ผ ์‚ญ์ œ
app.jsx ํด๋” ๋“ค์–ด๊ฐ€์„œ return ๋ฌธ ์•ˆ์— ์ „๋ถ€ ๋‹ค ๋น„์›Œ์ค€๋‹ค.
index.css์™€ App.css ์•ˆ์˜ ๊ธฐ๋ณธ ์ฝ”๋“œ๋“ค์„ ์ „๋ถ€ ์‚ญ์ œํ•ด์ค€๋‹ค.
main.jsx์—์„œ๋Š” StrictMode๋ฅผ ์ œ๊ฑฐํ•ด์ค€๋‹ค.

UI ๊ตฌํ˜„ํ•˜๊ธฐ

Viewer.jsx ํŒŒ์ผ์— ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์ฝ”๋“œ๋ฅผ ๋„ฃ์–ด์ฃผ๊ณ  App.jsx์—์„œ Viewer.jsx๋ฅผ import ํ•˜๋ฉด

npm run dev๋ฅผ ํ•ด๋ณด๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์€ ํ™”๋ฉด์ด ๋œฌ๋‹ค.

๋‹ค์Œ์œผ๋กœ Controller.jsxํŒŒ์ผ์— ๋ฒ„ํŠผํƒœ๊ทธ๋ฅผ ์ž‘์„ฑํ•œ๋‹ค.

App.jsx์— Controller๋ฅผ importํ•ด์ค€ ํ›„, Viewer์™€ Controller ๋ฅผ ๋‚˜๋ˆ„๊ธฐ ์œ„ํ•ด section์„ ๋‚˜๋ˆ„๋Š” ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•œ๋‹ค.

npm run dev๋ฅผ ํ•ด์ฃผ๋ฉด

๋‹ค์Œ๊ณผ ๊ฐ™์€ ํ™”๋ฉด์„ ๊ตฌํ˜„ ํ•  ์ˆ˜ ์žˆ๋‹ค.


css๋ฅผ ์ง€์ •ํ•˜๊ธฐ ์œ„ํ•ด App.css์— ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ž‘์„ฑํ•œ ํ›„,


App.jsx์—์„œ <div className="App">๋ฅผ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ž‘์„ฑํ•ด์ฃผ๋ฉด,

์ด๋Ÿฌํ•œ ํ™”๋ฉด์ด ๋œฌ๋‹ค.

๊ธฐ๋Šฅ ๊ตฌํ˜„ํ•˜๊ธฐ

App.jsx์— [count, setCount]๋ฅผ ์ž‘์„ฑํ•œ๋‹ค. =>props๋กœ ์ƒ์†๊ด€๊ณ„๋ฅผ ์ด์šฉํ•œ ๊ฐ’์„ ์ „๋‹ฌํ•ด์•ผํ•˜๊ธฐ ๋•Œ๋ฌธ์—!

App.jsx

import "./App.css";
import Viewer from "./components/Viewer";
import Controller from "./components/Controller";
import { useState } from "react";

function App() {
  const [count, setCount] = useState(0);

  const onClickButton = (value) => {
    setCount(count + value); //Controller์˜ ๋ช…๋ น์„ ๋ฐ›์•„์™€ ํ˜„์žฌ ๊ฐ’๊ณผ ์„ ํƒ๋œ ๊ฐ’์„ ๊ณ„์‚ฐํ•˜๋Š” ์ฝ”๋“œ!
  };

  return (
    <div className="App">
      <h1>Simple Counter</h1>
      <section>
        <Viewer count={count} />
      </section>
      <section>
        <Controller onClickButton={onClickButton} />
      </section>
    </div>
  );
}

export default App;

Viewer.jsx

const Viewer = ({count}) => {
  return (
    <div>
      <div>ํ˜„์žฌ ์นด์šดํŠธ</div>
      <h1>{count}</h1>
    </div>
  );
};

export default Viewer;

Controller.jsx

const Controller = ({ onClickButton }) => {
  return (
    <div>
      <button
        onClick={() => {
          onClickButton(-1); //props๋กœ ์ „๋‹ฌ๋ฐ›์•˜๋‹ค. 
        }}
      >
        -1
      </button>

      <button
        onClick={() => {
          onClickButton(-10);
        }}
      >
        -10
      </button>
      <button
        onClick={() => {
          onClickButton(-100);
        }}
      >
        -100
      </button>
      <button
        onClick={() => {
          onClickButton(+1);
        }}
      >
        +1
      </button>
      <button
        onClick={() => {
          onClickButton(+10);
        }}
      >
        +10
      </button>
      <button
        onClick={() => {
          onClickButton(+100);
        }}
      >
        +100
      </button>
    </div>
  );
};

export default Controller;

์ตœ์ข…์ ์œผ๋กœ ๋ฒ„ํŠผ์„ ํด๋ฆญํ•˜๋ฉด ์นด์šดํ„ฐ๋ฅผ ๊ณ„์‚ฐํ•ด์ฃผ๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์™„์„ฑํ•˜์˜€๋‹ค.

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