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

JiYoung Dev ๐Ÿ–ฅ

[React] React ๊ฐœ์š” ๋ฐ ์„ค์น˜, JSX ๋ฌธ๋ฒ• (2023.05.15) ๋ณธ๋ฌธ

full stack/React

[React] React ๊ฐœ์š” ๋ฐ ์„ค์น˜, JSX ๋ฌธ๋ฒ• (2023.05.15)

Shinjio 2023. 5. 16. 01:53

๐ŸŽˆ ๋ฆฌ์•กํŠธ๋ž€?

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์ค‘ ํ•˜๋‚˜

ํ”„๋ ˆ์ž„์›Œํฌ์ ์ธ ์„ฑ๊ฒฉ์„ ๋„๊ณ  ์žˆ์Œ

 

 

๐ŸŽˆ ๋ฆฌ์•กํŠธ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ์ด์œ 

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋งŒ์œผ๋กœ๋Š” ๋ถ€์กฑํ•˜๋‹ˆ๊นŒ! 

 

๐Ÿ“– ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์˜ ๋‹จ์ 

1. HTML๊ณผ JS๊ฐ€ ๊ฐ€์ง„ ๋ถ„๋ฆฌ๊ฐ

- JS์—์„œ HTML์— ์ ‘๊ทผํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” document. ์œผ๋กœ ์ ‘๊ทผํ•ด์•ผ ํ•จ

> ๊ด€๋ จ์žˆ๋Š” HTML, JS ๋ฌธ์„œ๋“ค์„ JSX๋กœ ํ•ฉ์ณ์คŒ!

 

2. ์ง๊ด€์ ์ด์ง€๋งŒ ๋„ˆ๋ฌด ๊ธด ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๋ฌธ๋ฒ•

 

3. ์ƒˆ ํŽ˜์ด์ง€๋ฅผ ๋“ค์–ด๊ฐˆ ๋•Œ๋งˆ๋‹ค ์ƒˆ๋กœ๊ณ ์นจ 

- ์‚ฌ์šฉ์ž๊ฐ€ ํ”ผ๋กœ๋„๋ฅผ ๋Š๋ผ๊ฒŒ ๋จ

> ์ƒˆ๋กœ์šด ํŽ˜์ด์ง€๋‚˜ ๋ฉ”๋‰ด๋ฅผ ๋“ค์–ด๊ฐˆ๋–„ ์ƒˆ๋กœ๊ณ ์นจ์ด ๋˜์ง€ ์•Š์•„ ์‚ฌ์šฉ์ž์˜ ํ”ผ๋กœ๋„๋ฅผ ์ค„์—ฌ์คŒ : Single Page Application(SPA) 

 

๐ŸŽˆ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ vs ํ”„๋ ˆ์ž„์›Œํฌ

๊ฐœ๋ฐœ์„ ๋„์™€์ฃผ๋Š” ์—ญํ• ์ด์ง€๋งŒ ๊ฐœ๋ฐœ์˜ ์ฃผ์ฒด๊ฐ€ ๋‹ค๋ฆ„

๐Ÿ“– ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ

๊ฐœ๋ฐœ์˜ ์ฃผ์ฒด๋Š” ๋‚˜!

๊ธฐ๋Šฅ๋งŒ ๊ฐ€์ ธ๋‹ค ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ

์ž์œ ๋„๊ฐ€ ๋†’๋‹ค

์žฅ์  - ๋ฒ„์ „์ด ์—…๋ฐ์ดํŠธ ๋˜๋”๋ผ๋„ ์ด์ „ ๋ฒ„์ „๊ณผ ํ˜ธํ™˜์ด ์ž˜ ๋จ!

ex. ๋ฆฌ์•กํŠธ - ํ”„๋ ˆ์ž„์›Œํฌ์  ์„ฑ๊ฒฉ์ด ๊ฐ•ํ•˜์ง€๋งŒ meta(๋งŒ๋“  ํšŒ์‚ฌ)์—์„œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ผ๊ณ  ์ •์˜ํ•จ

 

๐Ÿ“– ํ”„๋ ˆ์ž„์›Œํฌ

๊ฐœ๋ฐœ์˜ ์ฃผ์ฒด๋Š” ํ”„๋ ˆ์ž„์›Œํฌ!

์ œ๊ณต๋œ ํ‹€ ์•ˆ์œผ๋กœ ๋“ค์–ด๊ฐ€ ์ฃผ์–ด์ง„ ๊ทœ์น™์„ ์ง€์ผœ๊ฐ€๋ฉฐ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ

์ž์œ ๋„๊ฐ€ ๋‚ฎ๋‹ค

ex. ์•ต๊ธ€๋Ÿฌ, ๋ทฐ

์žฅ์  - ์‚ฌ์šฉํ•˜๊ธฐ ์‰ฝ๋‹ค

๋‹จ์  - ๋ฒ„์ „์ด ์—…๋ฐ์ดํŠธ ๋˜๋ฉด ์ด์ „ ๋ฒ„์ „๊ณผ ํ˜ธํ™˜์ด ์•ˆ๋˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ์žˆ์Œ

 

๐ŸŽˆ ๋ฆฌ์•กํŠธ์˜ ์—ญ์‚ฌ

 

 

๋ฌธ์„œ ์ •๋ฆฌ๊ฐ€ ๊ต‰์žฅํžˆ ์ž˜ ๋˜์–ด ์žˆ์Œ!

์›ํ™œํ•œ ์ปค๋ฎค๋‹ˆํ‹ฐ ํ˜•์„ฑ >> ๋‹ค์–‘ํ•œ ๋ฌธ์„œ ์ œ๊ณต(๊ณต์‹๋ฌธ์„œ๋„ ์ •๋ฆฌ๊ฐ€ ์ž˜ ๋˜์–ด ์žˆ์Œ)

 

์—ฌ๋Ÿฌ ์žฅ์ ์œผ๋กœ ๊ณ„์†ํ•ด์„œ React์˜ ์ธ๊ธฐ๋Š” ์ƒ์Šนํ•˜๊ณ  ์žˆ์Œ

๋Œ€์ฒด์ ์ธ ์›น ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์€ ๋ฆฌ์•กํŠธ๋กœ ๋งŒ๋“ค์–ด์ง 

React Native๋กœ ๋ชจ๋ฐ”์ผ ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜๊นŒ์ง€ ์ œ์ž‘ ๊ฐ€๋Šฅ

 

๐ŸŽˆ React ์„ค์น˜

๐Ÿ“– ์„œ๋ฒ„ ํ™˜๊ฒฝ Node.js ์„ค์น˜

 

๐Ÿ“– ๋น„์ฃผ์–ผ ์ŠคํŠœ๋””์˜ค ์ฝ”๋“œ react ํ”„๋กœ์ ํŠธ ์ƒ์„ฑ

1. ๋ฐ”ํƒ•ํ™”๋ฉด ํ˜น์€ ๋ณธ์ธ์ด ๊ณผ๋ชฉ์„ ๋ชจ์•„๋‘๋Š” ํด๋”์— 'React' ๋ผ๋Š” ํด๋” ์ƒ์„ฑ

2. Terminal -> new Terminal

3. ๋ฆฌ์•กํŠธ ํ”„๋กœ์ ํŠธ ์ƒ์„ฑํ•˜๊ธฐ
npx create-react-app project01
- npx : JS ํŒจํ‚ค์ง€ ๊ด€๋ฆฌ ๋ชจ๋“ˆ๋กœ ํ•„์š”ํ•œ ๊ธฐ๋Šฅ์„ ์„ค์น˜ ํ•  ์ˆ˜ ์žˆ๋‹ค.
- create-react-app : react ํ”„๋กœ์ ํŠธ ์ƒ์„ฑํ•ด์ค„๊ฒŒ์š”
- project01 : ํ”„๋กœ์ ํŠธ ์ด๋ฆ„

4. Happy Hacking!

Q. ๋งŒ์•ฝ, npx๊ฐ€ ์•ˆ๋œ๋‹ค?
1) cmd ์ฐฝ์„ ์ผœ์„œ node -v ํ™•์ธ
2) cmd ์ฐฝ์„ ์ผœ์„œ ๊ฒฝ๋กœ๋ฅผ ์ž…๋ ฅ - ํด๋” ์ฃผ์†Œ ์ฐฝ ๊ทธ๋Œ€๋กœ Ctrl + C (C:\Users\SMHRD\Desktop\React)

- cd : change directory

 

๐Ÿ“– ํ”„๋กœ์ ํŠธ ์‹œ์ž‘ํ•˜๊ธฐ

1. cd project01

2. npm start

 

Q.์šฐ๋ฆฌ์˜ ์„œ๋ฒ„?
A. Node ์„œ๋ฒ„

ํ‚ค๋Š” ๋ฐฉ๋ฒ•์€? npm start

- ์Œค ์ €๋Š” ์•ˆ๋ผ์š”... -> ํด๋” ์„ค์ • ์ž˜๋ชปํ•œ๊ฑฐ project01 ๋‚ด๋ถ€์—์„œ

Q. ์ €๋Š” ์„œ๋ฒ„๋Š” ์•ˆ๊ป๋Š”๋ฐ ํ™”๋ฉด๋งŒ ๊บผ๋ฒ„๋ ธ์–ด์š”
A. localhost:3000 ์œผ๋กœ ๋“ค์–ด๊ฐ€์‹œ๋ฉด

 

โš™๏ธ ๊ตฌ์กฐ ๋œฏ์–ด๋ณด๊ธฐ

 

โš™๏ธ ๋””๋ ‰ํ† ๋ฆฌ ๊ธฐ๋ณธ ๊ตฌ์กฐ

 

 

html(index.html)์€ ํ•˜๋‚˜ >> Single Page Application

App.js์•ˆ์—์„œ ๋‚ด์šฉ(JSX)๋ณ€๊ฒฝ 

์ƒˆ ํŽ˜์ด์ง€๋ฅผ ๋„˜์–ด๊ฐˆ ๋•Œ๋งˆ๋‹ค ์ •๋ณด๋ฅผ ๊ฐ€์ ธ์˜ค์ง€ ์•Š์•„๋„ ๋œ๋‹ค>>์ƒˆ๋กœ๊ณ ์นจ์ด ํ•„์š”์—†๋‹ค!

 

โš™๏ธ App.js ๋ฌธ๋ฒ•

 

JS์™€ HTML์„ ํ•จ๊ป˜ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” JSX๊ตฌ์กฐ ๋ฅผ ์‚ฌ์šฉ!

 

 

>> ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์™€ HTML ํŒŒ์ผ์— ๋ถ„๋ฆฌ๊ฐ์ด ์—†์œผ๋ฏ€๋กœ ๊ฐ€๋…์„ฑ์ด ๋†’๊ณ  ์ž‘์„ฑํ•˜๊ธฐ ์‰ฝ๋‹ค

 

โš™๏ธ ๋ฌธ๋ฒ• ์ฐจ์ด์ 

 

1. 

์†Œ๋ฌธ์ž๋กœ ์‹œ์ž‘ : HTML ํƒœ๊ทธ

๋Œ€๋ฌธ์ž๋กœ ์‹œ์ž‘ : ๋‚ด๊ฐ€ ๋งŒ๋“  ํƒœ๊ทธ

 

2.

className

 

3. 

{} ํ‘œํ˜„์‹์œผ๋กœ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๋ฌธ๋ฒ• ์‚ฌ์šฉ

 

import './App.css';

function App() {


  /* 
   JSX : ํ•˜๋‚˜์˜ ํŒŒ์ผ ์•ˆ์— HTML ๋ฌธ๋ฒ•๊ณผ JS ๋ฌธ๋ฒ•์„ ๋™์‹œ์— ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๋Š” ํŒŒ์ผ ํ™•์žฅ์ž
   >> ์žฅ์  : document ๊ฐ์ฒด์— ๋ณ„๋„๋กœ ์ ‘๊ทผํ•˜์ง€ ์•Š์•„๋„ ๋จ
            ๋”ฐ๋ผ์„œ ๊ฐ€๋…์„ฑ์ด ์ข‹๊ณ  ๊ธฐ๋Šฅ๋ณ„๋กœ ๋ถ„๋ฅ˜ํ•˜๊ธฐ ์‰ฌ์›€

  ** ์ง€์ผœ์•ผํ•  ๊ทœ์น™!
  1. ์—ฌ๋Ÿฌ ์š”์†Œ๊ฐ€ ์žˆ๋‹ค๋ฉด ๋ฐ˜๋“œ์‹œ ํ•˜๋‚˜์˜ ๋ถ€๋ชจ ์š”์†Œ๋กœ ๊ฐ์‹ธ์ฃผ์–ด์•ผ ํ•จ
      +) div๊ฐ€ ์•„๋‹ˆ์–ด๋„ ์ƒ๊ด€์—†๋‹ค.
  2. ํ‘œํ˜„์‹ ์‚ฌ์šฉ ๊ฐ€๋Šฅ
      : ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๋ฌธ๋ฒ•์€ return๋ฌธ ์•„๋ž˜ {} ์•ˆ์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Œ
        ๋‹จ, ์ค‘๊ด„ํ˜ธ ๋‚ด์— ์‚ฌ์šฉํ•  ์ˆ˜ ์—†๋‹ค 
        >> ์กฐ๊ฑด์ด ํ•˜๋‚˜ ์ผ ๋•Œ - {} ์•ˆ์— &&์—ฐ์‚ฐ์ž ์‚ฌ์šฉ
        >> ์กฐ๊ฑด์ด ๋‘๊ฐœ ์ผ ๋•Œ - {} ์•ˆ์— ์‚ผํ•ญ์—ฐ์‚ฐ์ž( ์กฐ๊ฑด ? ์‹คํ–‰๋ฌธ1 : ์‹คํ–‰๋ฌธ2)
        >> ์กฐ๊ฑด์ด 3๊ฐœ ์ด์ƒ์ผ ๋•Œ - ์กฐ๊ฑด๋ฌธ์„ ํ‘œํ˜„์‹์ด ์•„๋‹Œ return ์œ„์ชฝ์—์„œ ์‚ฌ์šฉ
  
  3. ๊ธฐ์กด html ๋ฌธ๋ฒ•๊ณผ์˜ ์ฐจ์ด์ 
    a. class ๋Œ€์‹  className ์‚ฌ์šฉ
    b. htmlํƒœ๊ทธ(DOM์š”์†Œ)๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ๋Š” ๋ฌด์กฐ๊ฑด ์†Œ๋ฌธ์ž๋กœ ์‹œ์ž‘
        ๋Œ€๋ฌธ์ž๋Š” ๋”ฐ๋กœ ์“ธ ์ผ์ด ์žˆ์Œ!
    c. content๊ฐ€ ์—†๋”๋ผ๋„ ๋ํƒœ๊ทธ๋ฅผ ์ƒ๋žตํ•  ์ˆ˜ ์—†๋‹ค! < />

  4. ์Šคํƒ€์ผ๋ง ์ ์šฉ
    a. css์•ˆ์— ์ž‘์„ฑ
      - import ๋˜์–ด ์žˆ๋Š” css์— ์‚ฝ์ž…
    b. ๊ฐ์ฒด ํ˜•ํƒœ๋กœ ์ž‘์„ฑ
      - ๊ฐ์ฒด ํ˜•ํƒœ๋กœ ์›ํ•˜๋Š” ์Šคํƒ€์ผ์„ ์ž‘์„ฑํ•œ ํ›„ ์›ํ•˜๋Š” ํƒœ๊ทธ ์Šคํƒ€์ผ ์†์„ฑ์œผ๋กœ ๊ฐ์ฒด ์ž…๋ ฅ
        ๋‹จ, ๊ฐ์ฒด ์•ˆ์—์„œ๋Š” camelCase๋กœ ์ž‘์„ฑํ•ด์•ผํ•จ 
        (ex. background-color => backgroundColor)
      */


  const textStyle = {
    color : 'blue',
    fontWeight : '900'
  }

  let name = '๊ฐ•์˜ˆ์ง€'
  let state = 'close'

  // 3. ์กฐ๊ฑด๋ฌธ์ด 3๊ฐœ ์ด์ƒ์ผ ๋•Œ
  let text = ""
  if(name === '๊ฐ•ํƒœ์šฐ'){
    text = "DB";
  }else if(name === "์„ ์˜ํ‘œ"){
    text = "ํ”„๋ก ํŠธ์—”๋“œ";
  }else{
    text = "๋ฐฑ์—”๋“œ";
  }

  // index.js์— ์žˆ๋Š” React.StrictMode๋ฅผ ์ผœ ๋†“์œผ๋ฉด ์ฝ”๋“œ๊ฐ€ ๋‘๋ฒˆ์”ฉ ์‹คํ–‰๋จ! (๋ณด์•ˆ๋•Œ๋ฌธ)
  console.log('ํ˜„์žฌ ๋ฐฉ๋ฌธํ•œ ์‚ฌ๋žŒ์€' , name);

  return (
    <div className='container'>

        <p style={textStyle}>์ง€์˜๋‹˜์˜ ๋ฆฌ์•กํŠธ ํ™ˆํŽ˜์ด์ง€์— ์˜ค์‹ ๊ฒƒ์„ ํ™˜์˜ํ•ฉ๋‹ˆ๋‹ค!</p>
        {/* ์กฐ๊ฑด์ด ํ•˜๋‚˜์ผ ๋•Œ */}
        {/* {state === 'open' && <p>{name}๋‹˜ ๋ฐ˜๊ฐ‘์Šต๋‹ˆ๋‹ค!</p>} */}

        {/* ์กฐ๊ฑด์ด ๋‘๊ฐœ์ผ ๋•Œ */}
        {/* {name === '๊ฐ•์˜ˆ์ง„' ? <p>ํ™˜์˜ํ•ฉ๋‹ˆ๋‹ค!</p> : <p>์ฃ„์†กํ•ฉ๋‹ˆ๋‹ค. ์ด์šฉํ•˜์‹ค ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.</p>} */}
        
        {/* ์กฐ๊ฑด์ด 3๊ฐœ ์ด์ƒ์ผ ๋•Œ */}
        {text}

    </div>

  );
}

export default App;

 

์‹ค์Šต

function AppExample(){
    /* 
    (1) ์‚ฌ์šฉ์ž์—๊ฒŒ ์ด๋ฆ„์„ ์ž…๋ ฅ๋ฐ›์•„์ค€๋‹ค. - prompt
    (2) ํ˜„์žฌ ๋‚ ์งœ๋ฅผ ๊ฐ€์ง€๊ณ  ์˜ฌ ๊ฒƒ
    (3) ์กฐ๊ฑด์— ๋”ฐ๋ผ ๊ณ„์ ˆ์„ ๋ถ„๋ฅ˜ 
        - 3์›” ~ 5์›” : ๋ด„
        - 6์›” ~ 8์›” : ์—ฌ๋ฆ„
        - 9์›” ~ 11์›” : ๊ฐ€์„
        - 12์›” ~ 2์›” : ๊ฒจ์šธ
    (4) 
    2023-05-15 => h1ํƒœ๊ทธ
    ์ˆ˜ํ‰
    XXX๋‹˜ ์ง€๊ธˆ์€ ๋ด„์ž…๋‹ˆ๋‹ค. ์ข‹์€ ํ•˜๋ฃจ ๋ณด๋‚ด์„ธ์š”! :) 
    */

    // (2) ํ˜„์žฌ ๋‚ ์งœ ๊ฐ€์ง€๊ณ  ์˜ค๊ธฐ
    let today = new Date();
    console.log('today :', today);

    // a. ์—ฐ๋„ - ์›” - ์ผ๋งŒ ๋ฐ˜ ๋ฐ˜ํ™˜ : .tolocalDateString
    let date = today.toLocaleDateString();
    console.log(date);

    // b. ์›” ๋ฐ˜ํ™˜ : .getMonth()
    let month = today.getMonth() + 1;
    console.log(month);

    let name = prompt('์ด๋ฆ„์„ ์ž…๋ ฅํ•˜์„ธ์š”!');
    let season = '';
    if(month<3 || month === 12){
        season = "๊ฒจ์šธ"
    }else if(month<6){
        season = "๋ด„"
    }else if(month<9){
        season = "์—ฌ๋ฆ„"
    }else{
        season = "๊ฐ€์„"
    }

    return(
        <div>
           <h1>{date}</h1>
           <hr />
           <p>{name}๋‹˜ ์ง€๊ธˆ์€ {season}์ž…๋‹ˆ๋‹ค. ์ข‹์€ํ•˜๋ฃจ ๋ณด๋‚ด์„ธ์š”! :)</p>
        </div>
    )
}

export default AppExample

 

์ •๋ฆฌ

React
์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์ค‘ ํ•˜๋‚˜,
ํ”„๋ ˆ์ž„์›Œํฌ์ ์ธ ์„ฑ๊ฒฉ์„ ๋„๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.

- ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ vs ํ”„๋ ˆ์ž„์›Œํฌ
     ์ฃผ์ฒด ๋‚˜          ์ฃผ์ฒด ํ”„๋ ˆ์ž„์›Œํฌ

- ์ƒˆ๋กœ๊ณ ์นจ์ด ์ผ์–ด๋‚˜์ง€ ์•Š๋Š”๋‹ค.
Single Page Application

- npx create-react-app ํ”„๋กœ์ ํŠธ๋ช…
React ํ”„๋กœ์ ํŠธ ์ƒ์„ฑ

index.html => Single Page
index.js => ๋งํฌ! ์—ฐ๊ฒฐ
App.js => ๋‚ด๊ฐ€ ์‹ค์งˆ์ ์œผ๋กœ ์ˆ˜์ •
-> ํ™•์žฅ์ž : JSX JS + HTML

    HTML ๋ฌธ๋ฒ• ์˜†์— JS ๋ฌธ๋ฒ•์„ ์“ฐ๋ ค๋ฉด? {}
    => ํ‘œํ˜„์‹
if๋ฌธ ์‚ฌ์šฉ ๋ถˆ๊ฐ€
    =>์กฐ๊ฑด๋ฌธ? 1 && ์—ฐ์‚ฐ์ž
    => ์กฐ๊ฑด 2 ์‚ผํ•ญ์—ฐ์‚ฐ์ž
    => ์กฐ๊ฑด 3๊ฐœ ์ด์ƒ return() ์œ„์— if๋ฌธ์œผ๋กœ ์ž‘์„ฑ

- React project๋ฅผ ์‹คํ–‰
npm start
=> localhost:3000