한 입 크기로 잘아 먹는 리액트의 Project1 카운터 앱을 만들어보겠습니다.
이번달(23.05)안에 리액트 관련 프로젝트를 한개 만들겠다고 계획을 세워놨었는데 지금 27일인데 가능할까요?
목차는 다음과 같습니다.
목차
프로젝트 준비하기




1.src/index.js
import React from 'react';
import ReactDOM from 'react-dom/client';
import './index.css';
import App from './App';
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(<App />);
2.src/App.js
import './App.css';
import Viewer from "./component/Viewer";
function App() {
return (div className="App"></div>;
}
export default App;
UI 구현하기
UI(User Interface) : 사용자 인터페이스라는 뜻입니다. 쉽게 말해서 껍데기?


const Viewer = () => {
return (
<div>
<div>현재 카운트: </div>
<h1>0</h1>
</div>
);
};
export default Viewer;
import './App.css';
import Viewer from "./component/Viewer";
function App() {
return (
<div className="App">
<h1>Simple Counter</h1>
<section>
<Viewer />
</section>
</div>
);
}
export default App;
2행에서 Viewer 컴포넌트를 App의 자식으로 배치해주었습니다.
7행에서 h1태그로 'Simple Counter'이라는 제목을 지정해주었습니다.
8~10행에서 section태그에 묶여있는 Viewer는 Viewer.js에서 작성했던 내용을 말합니다.


const Controller = () => {
return (
<div>
<button>-1</button>
<button>-10</button>
<button>-100</button>
<button>+100</button>
<button>+10</button>
<button>+1</button>
</div>
);
};
export default Controller;
import './App.css';
import Controller from './component/Controller';
import Viewer from "./component/Viewer";
function App() {
return (
<div className="App">
<h1>Simple Counter</h1>
<section>
<Viewer />
</section>
<section>
<Controller />
</section>
</div>
);
}
export default App;

body{
padding:20px;
}
.App{
margin: 0 auto;
width: 500px;
}
//.App > section은 className=App 요소의 section 태그에만 적용됩니다.
.App > section{
padding:20px;
background-color:rgb(245,245,245);
border:1px solid rgb(240,240,240);
border-radius:5px;
margin-bottom:10px;
}

기능 구현하기
State는 반드시 컴포넌트 함수 안에 만들어야 합니다.
그리고 지금까지 만든 [카운터] 앱에는 App, Viewer, Controller 3개의 컴포넌트가 있습니다.
이 3개의 컴포넌트 중에 가장 적절한 경우가 무엇인지 살펴보겠습니다.
import { useState } from "react";
const Viewer = () => {
const [count, serCount]=useState(0);
return (
<div>
<div>현재 카운트: </div>
<h1>{count}</h1>
</div>
);
};
export default Viewer;
import './App.css';
import Controller from './component/Controller';
import Viewer from "./component/Viewer";
import {useState} from "react";
function App() {
const [count, setCount]=useState(0);
const handleSetCount=(value)=>{
setCount(count + value);
};
return (
<div className="App">
<h1>Simple Counter</h1>
<section>
<Viewer count={count} />
</section>
<section>
<Controller handleSetCount={handleSetCount} />
</section>
</div>
);
}
export default App;
Viewer.js를 다음과 같이 수정합니다.
const Viewer = ({ count }) => {
return (
<div>
<div>현재 카운트: </div>
<h1>{count}</h1>
</div>
);
};
export default Viewer;
Controller.js를 다음과 같이 수정합니다.
const Controller = ({handleSetCount}) => {
return (
<div>
<button onClick={() => handleSetCount(-1)}>-1</button>
<button onClick={() => handleSetCount(-10)}>-10</button>
<button onClick={() => handleSetCount(-100)}>-100</button>
<button onClick={() => handleSetCount(+100)}>+100</button>
<button onClick={() => handleSetCount(+10)}>+10</button>
<button onClick={() => handleSetCount(+1)}>+1</button>
</div>
);
};
export default Controller;
