๊ด€๋ฆฌ ๋ฉ”๋‰ด

๐Ÿ–ฅ dev-ruby

[React] ๋ฆฌ์•กํŠธ ์‹œ์ž‘ํ•˜๊ธฐ - ๊ฐœ๋ฐœ ํ™˜๊ฒฝ ์„ค์ • | ์ƒˆ๋กœ์šด app ๋งŒ๋“ค๊ธฐ ๋ณธ๋ฌธ

React

[React] ๋ฆฌ์•กํŠธ ์‹œ์ž‘ํ•˜๊ธฐ - ๊ฐœ๋ฐœ ํ™˜๊ฒฝ ์„ค์ • | ์ƒˆ๋กœ์šด app ๋งŒ๋“ค๊ธฐ

ruby_s 2021. 11. 20. 23:30
728x90
๋ฐ˜์‘ํ˜•
SMALL

create-react-app์œผ๋กœ ํ”„๋กœ์ ํŠธ ์ƒ์„ฑ

Node.js ์„ค์น˜

๋ฆฌ์•กํŠธ ํ”„๋กœ์ ํŠธ๋ฅผ ์ƒ์„ฑํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” Node.js๊ฐ€ ๋จผ์ € ์„ค์น˜๋˜์–ด ์žˆ์–ด์•ผ ํ•œ๋‹ค.

 

๋‹ค์šด๋งํฌ

https://nodejs.org/ko/download/

์„ค์น˜ ํ›„, ํ„ฐ๋ฏธ๋„์—์„œ node -v or npm -v ๋ช…๋ น์–ด๋กœ ๋ฒ„์ „์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

 

create-react-app์œผ๋กœ ํ”„๋กœ์ ํŠธ ์ƒ์„ฑ

ํ•„์š”ํ•œ ์„ค์ • ๊ณผ์ •์„ ์ƒ๋žตํ•˜๊ณ  ๋ฐ”๋กœ ํ”„๋กœ์ ํŠธ ์ž‘์—… ํ™˜๊ฒฝ์„ ๊ตฌ์ถ•ํ•ด ์ฃผ๋Š” ๋„๊ตฌ

npx

npx๋Š” npm์˜ 5.2.0 ๋ฒ„์ ผ๋ถ€ํ„ฐ ์ƒˆ๋กœ ์ถ”๊ฐ€๋œ ๋„๊ตฌ์ด๋‹ค.

npx๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š์•˜์„ ๊ฒฝ์šฐ์—๋Š” npm์„ ํ†ตํ•ด react app ์„ ์ƒ์„ฑํ–ˆ์ง€๋งŒ,

> npm install -g create-react-app

-g ๋ฅผ ํ†ตํ•ด ์ „์—ญ์ ์œผ๋กœ create-react-app ๊ฐ€ ์„ค์น˜ํ•จ์œผ๋กœ์„œ ์—ฌ๋Ÿฌ ๋ฌธ์ œ์ ์ด ์•ผ๊ธฐ๋˜๋ฏ€๋กœ

 

npm ์„ ํ†ตํ•ด npx ๋ฅผ ์„ค์น˜ํ•˜์ž.

> npm install npx -g

 

์„ค์น˜ ํ›„, npx ๋ฅผ ํ†ตํ•ด react app ์„ ์ƒ์„ฑํ•˜์ž.

> npx create-react-app [app ์ด๋ฆ„]


app ์ด๋ฆ„์œผ๋กœ ๋œ ํด๋”๋กœ ๋“ค์–ด๊ฐ€๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์€ ํŒŒ์ผํŠธ๋ฆฌ ๊ตฌ์กฐ๋ฅผ ๊ฐ€์ง„๋‹ค.

.
โ”œโ”€โ”€ README.md
โ”œโ”€โ”€ node_modules
โ”œโ”€โ”€ package.json
โ”œโ”€โ”€ public
โ””โ”€โ”€ src

React App Start

์ƒ์„ฑํ•œ ๋””๋ ‰ํ„ฐ๋ฆฌ์—์„œ ๋‹ค์Œ ๋ช…๋ น์–ด๋ฅผ ์ž…๋ ฅํ•˜๋ฉด ์›น ๋ธŒ๋ผ์šฐ์ €๋กœ ํ”„๋กœ์ ํŠธ๋ฅผ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.

> npm start

์ดˆ๊ธฐ src/index.js

 

ํ•„์š”ํ•œ ๋ถ€๋ถ„ ๋‚จ๊ธฐ๊ณ  ์ „๋ถ€ ์ง€์šด๋‹ค.

import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';

ReactDOM.render(<App />, document.getElementById('root'));

src ๋””๋ ‰ํ† ๋ฆฌ ๋‚ด์˜ ํ•„์š”์—†๋Š” ํŒŒ์ผ ์‚ญ์ œ

 

์ตœ์ข…์ ์œผ๋กœ src ์—๋Š” ๋‹ค์Œ๋งŒ ์กด์žฌํ•œ๋‹ค.

src
โ”œโ”€โ”€ App.js
โ””โ”€โ”€ index.js

App.js ๋„ ํ•„์š”ํ•œ ๋ถ€๋ถ„๋งŒ ๋‚จ๊ธด๋‹ค.

import React from 'react';

function App() {
  return (
    <div className="App">

    </div>
  );
}

export default App;

App.js ์—์„œ ์ž‘์„ฑํ•œ ๊ฒƒ์€ index.html ์˜ <div id="root"></div> ๋กœ ๋“ค์–ด๊ฐ€๊ฒŒ ๋œ๋‹ค.

 

index.js ํŒŒ์ผ

import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';

ReactDOM.render(<App />, document.getElementById('root'));

์—ฌ๊ธฐ์„œ App.jsํŒŒ์ผ ์•ˆ์— ์ž‘์„ฑํ•œ ๊ฒƒ์„ document.getElementById('root') ์œผ๋กœ ๋„ฃ์–ด์ฃผ๊ฒŒ ๋œ๋‹ค.

 

 

 

 

728x90
๋ฐ˜์‘ํ˜•
LIST