Search

React ์‹œ์ž‘ํ•˜๊ธฐ

React ์‹œ์ž‘ํ•˜๊ธฐ

React ํ”„๋กœ์ ํŠธ๋ฅผ ์‹œ์ž‘ํ•˜๋Š” ๋ฐฉ๋ฒ•

1.
CDN ์œผ๋กœ ์‹œ์ž‘ํ•˜๊ธฐ
2.
Node.js ์™€ npm ์œผ๋กœ ์‹œ์ž‘ํ•˜๊ธฐ

CDN ์œผ๋กœ ์‹œ์ž‘ํ•˜๊ธฐ

โ€ข
๊ฐœ๋ฐœ์šฉ CDN script
โ€ข
๋ฐฐํฌ์šฉ CDN script

๊ฐœ๋ฐœ์šฉ CDN script

<script crossorigin src="https://unpkg.com/react@18/umd/react.development.js"></script> <script crossorigin src="https://unpkg.com/react-dom@18/umd/react-dom.development.js"></script>
HTML
๋ณต์‚ฌ

๋ฐฐํฌ์šฉ CDN script

<script crossorigin src="https://unpkg.com/react@18/umd/react.production.min.js"></script> <script crossorigin src="https://unpkg.com/react-dom@18/umd/react-dom.production.min.js"></script>
HTML
๋ณต์‚ฌ

๋ฐ”๋ฒจ(babel) CDN script

<script src="https://unpkg.com/@babel/standalone/babel.min.js"></script>
HTML
๋ณต์‚ฌ

์˜ˆ์‹œ์ฝ”๋“œ

<!DOCTYPE html> <html> <head> <!-- React์™€ ReactDOM์„ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ ์œ„ํ•œ CDN --> <script src="https://unpkg.com/react@18/umd/react.development.js" crossorigin></script> <script src="https://unpkg.com/react-dom@18/umd/react-dom.development.js" crossorigin></script> <!-- Babel์„ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•œ CDN --> <script src="https://unpkg.com/@babel/standalone/babel.min.js"></script> </head> <body> <!-- React ์ปดํฌ๋„ŒํŠธ๋ฅผ ๋ Œ๋”๋งํ•  ์ปจํ…Œ์ด๋„ˆ --> <div id="mydiv"></div> <!-- Babel์„ ์‚ฌ์šฉํ•˜์—ฌ JSX ์ฝ”๋“œ๋ฅผ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋กœ ํŠธ๋žœ์ŠคํŒŒ์ผํ•˜๋Š” ์Šคํฌ๋ฆฝํŠธ --> <script type="text/babel"> // React ํ•จ์ˆ˜ ์ปดํฌ๋„ŒํŠธ ์ •์˜ function Hello() { return <h1>Hello World!</h1>; } // ๋ Œ๋”๋งํ•  ์ปจํ…Œ์ด๋„ˆ ์š”์†Œ ๊ฐ€์ ธ์˜ค๊ธฐ const container = document.getElementById('mydiv'); // ReactDOM.createRoot๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ฃจํŠธ ์ƒ์„ฑ const root = ReactDOM.createRoot(container); // Hello ์ปดํฌ๋„ŒํŠธ๋ฅผ ๋ฃจํŠธ์— ๋ Œ๋”๋ง root.render(<Hello />); </script> </body> </html>
HTML
๋ณต์‚ฌ
CDN์„ ์‚ฌ์šฉํ•˜๋ฉด ๋ณ„๋„์˜ ๋นŒ๋“œ ๋ฐ ๋ฒˆ๋“ค๋ง ๊ณผ์ •์„ ๊ฑฐ์น˜์ง€ ์•Š์•„๋„ ๋˜๊ธฐ ๋•Œ๋ฌธ์— ์ดˆ๊ธฐ ๋‹จ๊ณ„์—์„œ๋Š” ํŽธ๋ฆฌํ•ฉ๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ, ์‹ค์ œ ํ”„๋กœ๋•์…˜ ํ™˜๊ฒฝ์—์„œ๋Š” ์„ฑ๋Šฅ, ์ตœ์ ํ™”, ์ฝ”๋“œ ๋ถ„ํ•  ๋“ฑ์„ ๊ณ ๋ คํ•ด์•ผ ํ•˜๋ฏ€๋กœ ์ผ๋ฐ˜์ ์œผ๋กœ ๋ฒˆ๋“ค๋Ÿฌ์™€ ํ•จ๊ป˜ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ๊ถŒ์žฅ๋ฉ๋‹ˆ๋‹ค.

Node.js ์™€ npm ์œผ๋กœ ์‹œ์ž‘ํ•˜๊ธฐ

1.
Node.js ์„ค์น˜
2.
React ํ”„๋กœ์ ํŠธ ์ƒ์„ฑ ๋ช…๋ น
3.
React ํ”„๋กœ์ ํŠธ ์‹คํ–‰

Node.js ์„ค์น˜

์‚ฌ์ „ ์„ค์น˜ : Node.js

React ํ”„๋กœ์ ํŠธ ์ƒ์„ฑ ๋ช…๋ น

npx create-react-app (ํ”„๋กœ์ ํŠธ๋ช…)

npx create-react-app react-app
Shell
๋ณต์‚ฌ

npm ์—์„œ network read ECONNRESET์ด ๋ฐœ์ƒํ•  ๋•Œ ํ•ด๊ฒฐ ๋ฐฉ๋ฒ•

npm config set registry http://registry.npmjs.org/
Shell
๋ณต์‚ฌ
registry ์„ค์ • ํ›„, ๋‹ค์‹œ ๋ช…๋ น์–ด ์‹คํ–‰.

React ํ”„๋กœ์ ํŠธ ์‹คํ–‰

ํ”„๋กœ์ ํŠธ ๊ฒฝ๋กœ๋กœ ์ด๋™

cd react-app
Shell
๋ณต์‚ฌ

ํ”„๋กœ์ ํŠธ ์‹คํ–‰

npm start
Shell
๋ณต์‚ฌ

์ฃผ์š” ์†Œ์Šค ํŒŒ์ผ

1.
index.html
2.
index.js
3.
app.js

index.html

<!DOCTYPE html> <html lang="en"> <head> <!-- ๋ฌธ์„œ์˜ ๋ฌธ์ž ์ธ์ฝ”๋”ฉ์„ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค. --> <meta charset="utf-8" /> <!-- ์›น์‚ฌ์ดํŠธ์˜ ์•„์ด์ฝ˜์„ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค. --> <link rel="icon" href="%PUBLIC_URL%/favicon.ico" /> <!-- ๋ทฐํฌํŠธ ์„ค์ •์œผ๋กœ ๋ฐ˜์‘ํ˜• ์›น ๋””์ž์ธ์„ ์ง€์›ํ•ฉ๋‹ˆ๋‹ค. --> <meta name="viewport" content="width=device-width, initial-scale=1" /> <!-- ํ…Œ๋งˆ ์ƒ‰์ƒ์„ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค. --> <meta name="theme-color" content="#000000" /> <!-- ์›น ์•ฑ ์„ค๋ช…์„ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค. --> <meta name="description" content="Web site created using create-react-app" /> <!-- ์• ํ”Œ ํ„ฐ์น˜ ์•„์ด์ฝ˜์„ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค. --> <link rel="apple-touch-icon" href="%PUBLIC_URL%/logo192.png" /> <!-- manifest.json์€ ์›น ์•ฑ์ด ์‚ฌ์šฉ์ž์˜ ๋ชจ๋ฐ”์ผ ๊ธฐ๊ธฐ๋‚˜ ๋ฐ์Šคํฌํ†ฑ์— ์„ค์น˜๋  ๋•Œ ์‚ฌ์šฉ๋˜๋Š” ๋ฉ”ํƒ€๋ฐ์ดํ„ฐ๋ฅผ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. https://developers.google.com/web/fundamentals/web-app-manifest/ ์ฐธ๊ณ  --> <link rel="manifest" href="%PUBLIC_URL%/manifest.json" /> <!-- ์œ„์˜ ํƒœ๊ทธ๋“ค์—์„œ %PUBLIC_URL%์„ ์ฃผ๋ชฉํ•˜์„ธ์š”. ๋นŒ๋“œ ์ค‘์—๋Š” ์ด๊ฒƒ์ด `public` ํด๋”์˜ URL๋กœ ๋Œ€์ฒด๋ฉ๋‹ˆ๋‹ค. ์˜ค์ง `public` ํด๋” ๋‚ด๋ถ€์˜ ํŒŒ์ผ๋งŒ HTML์—์„œ ์ฐธ์กฐํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. "/favicon.ico" ๋˜๋Š” "favicon.ico"์™€ ๋‹ฌ๋ฆฌ "%PUBLIC_URL%/favicon.ico"์€ ํด๋ผ์ด์–ธํŠธ ์‚ฌ์ด๋“œ ๋ผ์šฐํŒ…๊ณผ ๋ฃจํŠธ๊ฐ€ ์•„๋‹Œ ๊ณต์šฉ URL ๋ชจ๋‘์—์„œ ์˜ฌ๋ฐ”๋ฅด๊ฒŒ ์ž‘๋™ํ•ฉ๋‹ˆ๋‹ค. ๋ฃจํŠธ๊ฐ€ ์•„๋‹Œ ๊ณต์šฉ URL์„ ๊ตฌ์„ฑํ•˜๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•œ ์ž์„ธํ•œ ๋‚ด์šฉ์€ `npm run build`๋ฅผ ์‹คํ–‰ํ•˜์—ฌ ํ™•์ธํ•˜์„ธ์š”. --> <!-- ์›น ํŽ˜์ด์ง€์˜ ์ œ๋ชฉ์„ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค. --> <title>React App</title> </head> <body> <!-- JavaScript๋ฅผ ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ๋„๋ก JavaScript๋ฅผ ํ™œ์„ฑํ™”ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. --> <noscript>You need to enable JavaScript to run this app.</noscript> <!-- React ์•ฑ์„ ๋ Œ๋”๋งํ•  ์ปจํ…Œ์ด๋„ˆ ์—ญํ• ์„ ํ•˜๋Š” div ์š”์†Œ์ž…๋‹ˆ๋‹ค. --> <div id="root"></div> <!-- ์ด HTML ํŒŒ์ผ์€ ํ…œํ”Œ๋ฆฟ์ž…๋‹ˆ๋‹ค. ๋ธŒ๋ผ์šฐ์ €์—์„œ ์ง์ ‘ ์—ด๋ฉด ๋นˆ ํŽ˜์ด์ง€๊ฐ€ ํ‘œ์‹œ๋ฉ๋‹ˆ๋‹ค. ์ด ํŒŒ์ผ์— ์›นํฐํŠธ, ๋ฉ”ํƒ€ ํƒœ๊ทธ ๋˜๋Š” ์• ๋„๋ฆฌํ‹ฑ์Šค๋ฅผ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋นŒ๋“œ ๋‹จ๊ณ„์—์„œ๋Š” ๋ฒˆ๋“ค๋œ ์Šคํฌ๋ฆฝํŠธ๋ฅผ <body> ํƒœ๊ทธ์— ๋ฐฐ์น˜ํ•ฉ๋‹ˆ๋‹ค. ๊ฐœ๋ฐœ์„ ์‹œ์ž‘ํ•˜๋ ค๋ฉด `npm start` ๋˜๋Š” `yarn start`๋ฅผ ์‹คํ–‰ํ•˜์„ธ์š”. ํ”„๋กœ๋•์…˜ ๋ฒˆ๋“ค์„ ์ƒ์„ฑํ•˜๋ ค๋ฉด `npm run build` ๋˜๋Š” `yarn build`๋ฅผ ์‚ฌ์šฉํ•˜์„ธ์š”. --> </body> </html>
HTML
๋ณต์‚ฌ

index.js

import React from 'react'; import ReactDOM from 'react-dom/client'; import './index.css'; import App from './App'; import reportWebVitals from './reportWebVitals'; // 'root'๋ผ๋Š” ID๋ฅผ ๊ฐ€์ง„ DOM ์š”์†Œ๋ฅผ ์ฐพ์•„์„œ React Root๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. const root = ReactDOM.createRoot(document.getElementById('root')); // React StrictMode๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ App ์ปดํฌ๋„ŒํŠธ๋ฅผ ๋ Œ๋”๋งํ•ฉ๋‹ˆ๋‹ค. root.render( <React.StrictMode> <App /> </React.StrictMode> ); // ์•ฑ์˜ ์„ฑ๋Šฅ์„ ์ธก์ •ํ•˜๋ ค๋ฉด ๊ฒฐ๊ณผ๋ฅผ ๊ธฐ๋กํ•  ํ•จ์ˆ˜๋ฅผ ์ „๋‹ฌํ•ฉ๋‹ˆ๋‹ค. // (์˜ˆ: reportWebVitals(console.log)) // ๋˜๋Š” ๊ฒฐ๊ณผ๋ฅผ ๋ถ„์„ํ•˜๋Š” ๋ถ„์„ ๋„๊ตฌ์— ์ „๋‹ฌํ•ฉ๋‹ˆ๋‹ค. ์ž์„ธํ•œ ๋‚ด์šฉ์€ https://bit.ly/CRA-vitals๋ฅผ ์ฐธ์กฐํ•˜์„ธ์š”. reportWebVitals();
JavaScript
๋ณต์‚ฌ

app.js

// 'logo.svg' ํŒŒ์ผ์„ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค. import logo from './logo.svg'; // 'App.css' ์Šคํƒ€์ผ ํŒŒ์ผ์„ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค. import './App.css'; // ํ•จ์ˆ˜ํ˜• ์ปดํฌ๋„ŒํŠธ์ธ App์„ ์ •์˜ํ•ฉ๋‹ˆ๋‹ค. function App() { // ์ปดํฌ๋„ŒํŠธ์˜ JSX ๊ตฌ์กฐ๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. return ( <div className="App"> {/* ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ํ—ค๋” ๋ถ€๋ถ„์„ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค. */} <header className="App-header"> {/* ๋กœ๊ณ  ์ด๋ฏธ์ง€๋ฅผ ํ‘œ์‹œํ•ฉ๋‹ˆ๋‹ค. */} <img src={logo} className="App-logo" alt="logo" /> {/* ํ…์ŠคํŠธ ๋‚ด์šฉ์„ ํ‘œ์‹œํ•˜๊ณ , ์ˆ˜์ • ์‹œ ๋ฆฌ๋กœ๋“œ ์•ˆ๋‚ด๋ฅผ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. */} <p> Edit <code>src/App.js</code> and save to reload. </p> {/* React ๊ณต์‹ ์›น์‚ฌ์ดํŠธ๋กœ ์ด๋™ํ•˜๋Š” ๋งํฌ๋ฅผ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. */} <a className="App-link" href="https://reactjs.org" target="_blank" rel="noopener noreferrer" > Learn React </a> </header> </div> ); } // App ์ปดํฌ๋„ŒํŠธ๋ฅผ ๋ชจ๋“ˆ๋กœ ๋‚ด๋ณด๋ƒ…๋‹ˆ๋‹ค. export default App;
JavaScript
๋ณต์‚ฌ

์‹คํ–‰์ˆœ์„œ ์ดํ•ดํ•˜๊ธฐ

์ง„์ž…์ 

create-react-app์—์„œ React ์•ฑ์˜ ์ง„์ž…์ (entry point)์€ src/index.js๋กœ ์„ค์ •๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๊ฒƒ์€ create-react-app์ด ์ œ๊ณตํ•˜๋Š” ๊ธฐ๋ณธ ๊ตฌ์กฐ์—์„œ์˜ ๊ทœ์น™ ์ค‘ ํ•˜๋‚˜์ž…๋‹ˆ๋‹ค.
์ผ๋ฐ˜์ ์œผ๋กœ package.json ํŒŒ์ผ ๋‚ด๋ถ€์—์„œ "main" ํ•„๋“œ๋กœ ์ง„์ž…์ ์„ ์ง€์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ create-react-app์—์„œ๋Š” ๋Œ€๋ถ€๋ถ„์˜ ์„ค์ •์ด ๋‚ด๋ถ€์ ์œผ๋กœ ์ด๋ฃจ์–ด์ง€๊ธฐ ๋•Œ๋ฌธ์— ๊ฐœ๋ฐœ์ž๊ฐ€ ์ง์ ‘์ ์œผ๋กœ "main" ํ•„๋“œ๋ฅผ ์„ค์ •ํ•  ํ•„์š”๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค.

์‹คํ–‰ ์ˆœ์„œ

1.
public/index.html
2.
src/index.js
a.
App.js ์ปดํฌ๋„ŒํŠธ ์ƒ์„ฑ
3.
JSX JavaScript ๋กœ ๋ณ€ํ™˜, DOM ์— ๋ Œ๋”๋ง

public/index.html

๋ธŒ๋ผ์šฐ์ €์—์„œ public/index.html ํŒŒ์ผ์ด ์—ด๋ฆฌ๋ฉด, ๊ทธ ์•ˆ์— ์žˆ๋Š” <div id="root"></div>๋Š” React ์•ฑ์ด ๋ Œ๋”๋ง๋  ์žฅ์†Œ๋ฅผ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค.

src/index.js

๊ทธ ํ›„, index.js ํŒŒ์ผ์ด ์‹คํ–‰๋ฉ๋‹ˆ๋‹ค. index.js์—์„œ๋Š” ReactDOM.render()๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ App ์ปดํฌ๋„ŒํŠธ๋ฅผ <div id="root"></div>์— ๋ Œ๋”๋งํ•ฉ๋‹ˆ๋‹ค.
ReactDOM.render(<App />, document.getElementById('root')); ์ด ๋ถ€๋ถ„์ด ์•ฑ์„ ์‹œ์ž‘ํ•˜๋Š” ์ง€์ ์ž…๋‹ˆ๋‹ค.
React๋Š” ์ดํ›„์— App.js ํŒŒ์ผ์—์„œ ์ •์˜๋œ ์ปดํฌ๋„ŒํŠธ๋ฅผ ์ƒ์„ฑํ•˜๊ณ , JSX๋ฅผ JavaScript๋กœ ๋ณ€ํ™˜ํ•˜์—ฌ ํ•ด๋‹น DOM ์š”์†Œ์— ๋ Œ๋”๋งํ•ฉ๋‹ˆ๋‹ค.

์ˆ˜๋™์œผ๋กœ ํ”„๋กœ์ ํŠธ ์‹œ์ž‘ํ•˜๊ธฐ

1.
ํ”„๋กœ์ ํŠธ ์ดˆ๊ธฐํ™”
2.
App ์ปดํฌ๋„ŒํŠธ ์ž‘์„ฑ
3.
๋ฉ”์ธ ํ™”๋ฉด ๊ตฌํ˜„
4.
HTML ํŒŒ์ผ ์ž‘์„ฑ
5.
ํ”„๋กœ์ ํŠธ ์‹คํ–‰

ํ”„๋กœ์ ํŠธ ์ดˆ๊ธฐํ™”

ํ”„๋กœ์ ํŠธ ํด๋”์—์„œ ๋‹ค์Œ ๋ช…๋ น์–ด๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ React ๋ฐ ReactDOM์„ ์„ค์น˜ํ•ฉ๋‹ˆ๋‹ค.
npm init
HTML
๋ณต์‚ฌ
npm install react react-dom react-scripts
Shell
๋ณต์‚ฌ

package.json

"scripts": { "start": "react-scripts start", },
JSON
๋ณต์‚ฌ

App ์ปดํฌ๋„ŒํŠธ ์ž‘์„ฑ

App.js

// ํŒŒ์ผ: App.js import React from 'react'; // JSX๋ฅผ ์‚ฌ์šฉํ•œ ์ปดํฌ๋„ŒํŠธ ์ •์˜ const App = () => { return ( <div> <h1>Hello, JSX World!</h1> <p>This is a simple React component using JSX.</p> </div> ); }; export default App;
JavaScript
๋ณต์‚ฌ

๋ฉ”์ธ ํ™”๋ฉด ๊ตฌํ˜„

index.js

// ํŒŒ์ผ: index.js import React from 'react'; import ReactDOM from 'react-dom'; import App from './App'; // ๋ฉ”์ธ ํ™”๋ฉด์—์„œ App ์ปดํฌ๋„ŒํŠธ ๋ Œ๋”๋ง ReactDOM.render(<App />, document.getElementById('root'));
JavaScript
๋ณต์‚ฌ

HTML ํŒŒ์ผ ์ž‘์„ฑ

index.html

<!-- ํŒŒ์ผ: index.html --> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>React App</title> </head> <body> <!-- React ์•ฑ์„ ํ‘œ์‹œํ•  ์ปจํ…Œ์ด๋„ˆ --> <div id="root"></div> </body> </html>
HTML
๋ณต์‚ฌ

ํ”„๋กœ์ ํŠธ ์‹คํ–‰

npm start
HTML
๋ณต์‚ฌ

์ฐธ๊ณ 

React ๋ฒ„์ „ ํ™•์ธ ๋ช…๋ น

npm list react
JSON
๋ณต์‚ฌ