전화번호부를 하나의 컴포넌트에서 만들어보며 React의 기초 문법을 배워봅시다.
프로젝트를 진행하기 전, 우선 index.js 파일에 대해서 알아볼 필요가 있습니다.
import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';
import * as serviceWorker from './serviceWorker';
ReactDOM.render(<App />, document.getElementById('root'));
// If you want your app to work offline and load faster, you can change
// unregister() to register() below. Note this comes with some pitfalls.
// Learn more about service workers: <https://bit.ly/CRA-PWA>
serviceWorker.unregister();
위의 코드 중에서 가장 중요한 부분은 6번째 줄에 있습니다.
ReactDOM.render(<App />, document.getElementById('root'));
이 코드가 의미하는 것은 "App 컴포넌트를 root라는 id를 가진 element에 render하라"입니다. 그런데 어떤 파일에 있는 element에 렌더링할까요? public/index.html 파일로 들어가 봅시다.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<link rel="shortcut icon" href="%PUBLIC_URL%/favicon.ico" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="theme-color" content="#000000" />
<!--
manifest.json provides metadata used when your web app is installed on a
user's mobile device or desktop. See <https://developers.google.com/web/fundamentals/web-app-manifest/>
-->
<link rel="manifest" href="%PUBLIC_URL%/manifest.json" />
<!--
Notice the use of %PUBLIC_URL% in the tags above.
It will be replaced with the URL of the `public` folder during the build.
Only files inside the `public` folder can be referenced from the HTML.
Unlike "/favicon.ico" or "favicon.ico", "%PUBLIC_URL%/favicon.ico" will
work correctly both with client-side routing and a non-root public URL.
Learn how to configure a non-root public URL by running `npm run build`.
-->
<title>React App</title>
</head>
<body>
<noscript>You need to enable JavaScript to run this app.</noscript>
**<div id="root"></div>**
<!--
This HTML file is a template.
If you open it directly in the browser, you will see an empty page.
You can add webfonts, meta tags, or analytics to this file.
The build step will place the bundled scripts into the <body> tag.
To begin the development, run `npm start` or `yarn start`.
To create a production bundle, use `npm run build` or `yarn build`.
-->
</body>
</html>
중간에 파란색 배경색을 칠해놨고 볼드체로 강조해놓은 부분이 보이시나요? 바로 저기에 렌더링이 되는 것입니다. React는 프로젝트 내에서 일련의 과정을 거친 후, 저기 있는 div에 요소들을 추가시켜줍니다. 윗 문장에서 말한 일련의 과정은 Webpack과 Babel에서 수행되는 것으로 꽤 복잡하므로 건너뛰겠습니다. 그럼 index.js에서 App 컴포넌트를 렌더링한다고 했으니 그 파일에서 전화번호부 형태를 만들어봅시다.
import React from "react";
import "./App.css";
function App() {
return (
<div className="container">
<div className="input_boxes">
<div className="input_box">
<div className="input_box_name">이름</div>
<input
type="text"
placeholder="이름"
name="name"
className="input_box_input"
/>
</div>
<div className="input_box">
<div className="input_box_name">전화번호</div>
<input
type="text"
placeholder="전화번호"
name="phone"
className="input_box_input"
/>
</div>
<button className="input_box_button">저장</button>
</div>
<div className="phone_list">
<div className="phone_item">
<div className="phone_item_left">
<div className="phone_item_name">홍길동</div>
<div className="phone_item_phone">010-0000-0000</div>
</div>
<div className="phone_item_right">
<button>삭제</button>
</div>
</div>
<div className="phone_item">
<div className="phone_item_left">
<div className="phone_item_name">홍길동</div>
<div className="phone_item_phone">010-0000-0000</div>
</div>
<div className="phone_item_right">
<button>삭제</button>
</div>
</div>
<div className="phone_item">
<div className="phone_item_left">
<div className="phone_item_name">홍길동</div>
<div className="phone_item_phone">010-0000-0000</div>
</div>
<div className="phone_item_right">
<button>삭제</button>
</div>
</div>
</div>
</div>
);
}
export default App;