React/๊ธฐ์ดˆ

๋ฆฌ์•กํŠธ ์‹คํ–‰ ํ๋ฆ„ [ Index.js, App.js, Index.html ]

๋ฟŒ๋น„ 2024. 7. 12. 23:08
728x90
๐Ÿ”ฅ 3๊ฐœ์˜ ํŒŒ์ผ์€ creat-react-app ํ•˜๋ฉด ์ž๋™์œผ๋กœ ํŒŒ์ผ์ด ์ƒ์„ฑ๋œ๋‹ค
๐Ÿ”ฅ
public ํด๋” ์•ˆ์— index.html ํŒŒ์ผ์ด src ํด๋” ์•ˆ์— App.js, index.js ํŒŒ์ผ์ด ์žˆ๋‹ค

 

๐Ÿ”ฅ index.js

์ œ์ผ ์ƒ์œ„์ž์ด๋ฉฐ ๊ฐ€์žฅ ๋จผ์ € react์—์„œ ์‹คํ–‰๋˜๋Š” ํŒŒ์ผ์ด๋‹ค

๋ฉ”์ธ ํ”„๋กœ๊ทธ๋žจ์ด๋‹ค

// ํ•„์š”ํ•œ ๋ถ€๋ถ„๋งŒ ์ž‘์„ฑ
import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';

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

 

๐Ÿ”ฅ App.js

์ปดํฌ๋„ŒํŠธ๋ฅผ ์ •์˜ํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์ด๋‹ค

// ํ•„์š”ํ•œ ๋ถ€๋ถ„๋งŒ ์ž‘์„ฑ
function App(){    // App() >> <App/> 'JSX๋ฌธ๋ฒ•'
    return  <div>hi</div>;
}
export default App;

 

๐Ÿ”ฅ index.html

index.js์— ์˜ํ•ด ๋ Œ๋”๋ง ๋œ ๊ฒฐ๊ณผ๊ฐ€ ํ™”๋ฉด์— ๋ณด์ด๊ฒŒ ๋œ๋‹ค

// ํ•„์š”ํ•œ ๋ถ€๋ถ„๋งŒ ์ž‘์„ฑ
  <body>
    <div id="root"></div>
  </body>

 

๐Ÿ”ฅ package.json

ํ”„๋กœ์ ํŠธ๋ฅผ ๊ด€๋ฆฌํ•˜๋Š” ์„ค์ •ํŒŒ์ผ์ด๋ฉฐ json ํ˜•์‹์œผ๋กœ ๋˜์–ด์žˆ๋‹ค

  "scripts": {
    "start": "react-scripts start", // ๋ฆฌ์•กํŠธ ์‹คํ–‰
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },

๐Ÿ”ฅ ๋ Œ๋”๋ง ์ด๋ž€? : ์ฝ”๋“œ๋กœ ์ •์˜๋œ ๋‚ด์šฉ์ด ์‹ค์ œ ๋ธŒ๋ผ์šฐ์ € ํ™”๋ฉด์— ๊ทธ๋ ค์ง€๋Š” ๊ณผ์ •์ด๋‹ค

๐Ÿ”ฅ ์‹คํ–‰ ํ๋ฆ„ ์ดํ•ด 

  1. (npm start or yarn start)๋ฅผ ํ•˜๋ฉด package.json ํŒŒ์ผ ์•ˆ์— “react-scripts start” ๊ฐ€ ์‹คํ–‰๋œ๋‹ค
  2. react-scripts start๋Š” index.js ํŒŒ์ผ์„ ์‹คํ–‰์‹œํ‚จ๋‹ค
    1.  index.js ํŒŒ์ผ์—๋Š” ReactDOM.render() ํ•จ์ˆ˜๊ฐ€ ์žˆ๋Š”๋ฐ render > ๋ฌด์–ธ๊ฐ€๋ฅผ ์ถœ๋ ฅ์‹œํ‚จ๋‹ค๋Š” ๋œป์ด๋‹ค
    2. ํ•จ์ˆ˜ ์•ˆ์—๋Š” ๋‘ ๊ฐœ์˜ ํŒŒ๋ผ๋ฏธํ„ฐ๊ฐ€ <App/>, document.getElementById('root') ์ด๋ ‡๊ฒŒ ๋˜์–ด์žˆ๋‹ค 
      ํ’€์–ด์„œ ๋ณด๋ฉด ReactDOM.render() ํ•จ์ˆ˜๊ฐ€ <App/> ์ปดํฌ๋„ŒํŠธ๋ฅผ ์ถœ๋ ฅ์‹œํ‚ฌ ๊ฑด๋ฐ ์–ด๋””์—? document.getElementById('root') ์ด๊ณณ์—! ์ด๊ณณ์€ ์–ด๋””? index.html ํŒŒ์ผ ์•ˆ์— Id ๊ฐ€ root ์ธ ๊ณณ์—
    3. <App/>๋Š” App.js์ด๊ณ  document.getElementById('root')๋Š” index.html์ด๋‹ค

์ฆ‰ App.js ์•ˆ์— ์žˆ๋Š” ์ปดํฌ๋„ŒํŠธ๋“ค์„ index.html ํŒŒ์ผ์— id ๊ฐ€ root ์ธ ๊ณณ์— ๋ Œ๋”๋ง ์‹œํ‚จ๋‹ค๋Š” ์˜๋ฏธ์ด๋‹ค

โ€ป  ์š”์•ฝํ•˜์ž๋ฉด,  ReactDOM.render ํ•จ์ˆ˜,  ์ฒซ ๋ฒˆ์งธ ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ๋‘ ๋ฒˆ์งธ ํŒŒ๋ผ๋ฏธํ„ฐ์— ๋ Œ๋”๋ง ํ•ด๋ผ!

/** index.js ํŒŒ์ผ, ํ•„์š”ํ•œ ๋ถ€๋ถ„๋งŒ ์ž‘์„ฑ */
import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';

// index.js ์— ์žˆ๋Š” ์ฝ”๋“œ
ReactDOM.render(<App />, document.getElementById('root'));

// ์œ„์— ์ฝ”๋“œ๋ฅผ ํ’€์–ด์„œ ์ดํ•ด ํ•˜๋ ค๊ณ  ์ž‘์„ฑํ•œ ์ฝ”๋“œ
ReactDOM.render(
<body>
    <div id="root"> // index.html
	   <div>hi</div> // app.js ํŒŒ์ผ์•ˆ์— ์ปดํฌ๋„ŒํŠธ
    </div>
  </body>
);

 

๐Ÿ”ฅ ์‹คํ–‰ ํ๋ฆ„ 

1. npm start or yarn start(๋ฆฌ์•กํŠธ ์‹คํ–‰)

2. index.js๊ฐ€ ์‹คํ–‰๋˜๋ฉด์„œ ReactDOM.render() ํ•จ์ˆ˜๊ฐ€ ์‹คํ–‰ 

3. render() ํ•จ์ˆ˜๊ฐ€ <App/> ์ฆ‰, App.js ์— ์žˆ๋Š” ๋ชจ๋“  ์ปดํฌ๋„ŒํŠธ๋“ค์„ index.html ํŒŒ์ผ์— id๊ฐ€ "root"์ธ ๊ณณ์— ๊ทธ๋ฆฐ๋‹ค

4. ์ตœ์ข…์ ์œผ๋กœ ๋ชจ๋“  ์ปดํฌ๋„ŒํŠธ๋“ค์ด ๊ทธ๋ ค์ง„ index.html ํŒŒ์ผ์ด ํ™”๋ฉด์— ๋ Œ๋”๋ง ๋œ๋‹ค

์ฃผ์˜ โ˜… ์ด๋•Œ index.html ์— ์žˆ๋Š” id๋ฅผ ๋ฐ”๊พธ๋ฉด ์—๋Ÿฌ๊ฐ€ ๋‚œ๋‹ค


โ€ป Reference

https://jiwon709.tistory.com/21

[์œ ํŠœ๋ธŒ] https://www.youtube.com/watch?v=qCADzDFiVgo

 

728x90