ํ ์ ํฌ๊ธฐ๋ก ์๋ผ ๋จน๋ ๋ฆฌ์กํธ ์น์ 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๋ฅผ ์ ๊ฑฐํด์ค๋ค.
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;
์ต์ข
์ ์ผ๋ก ๋ฒํผ์ ํด๋ฆญํ๋ฉด ์นด์ดํฐ๋ฅผ ๊ณ์ฐํด์ฃผ๋ ํ๋ก๊ทธ๋จ์ ์์ฑํ์๋ค.
