Today I Learned

2024.01.17 TIL

Seo Ji Won 2024. 1. 17. 20:58

 ๐Ÿ’ก React

  • A JavaScript library for building user interface - React.js๋Š” SPA ๊ธฐ๋ฐ˜์˜ ํ”„๋ก ํŠธ์—”๋“œ ๊ฐœ๋ฐœ ํ”„๋ ˆ์ž„์›Œํฌ ์ค‘ ํ•˜๋‚˜๋กœ์„œ, ์ปดํฌ๋„ŒํŠธ ๋‹จ์œ„์˜ ๋…๋ฆฝ์ ์ธ ๋ธ”๋ก์„ ์ด์šฉํ•œ ๊ฐœ๋ฐœ ๋ฐฉ๋ฒ•์„ ์ด์šฉํ•œ๋‹ค.

 

SPA๋ž€?

Single Page Application, ํ•˜๋‚˜์˜ ํŽ˜์ด์ง€๋กœ ์ด๋ฃจ์–ด์ง„ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์ด๋ผ๋Š” ๋œป.

MPA(Multi Page Application)์™€ ์ƒ๋ฐ˜๋œ ๊ฐœ๋… SPA↔MPA

  • ์™œ ์ข‹์„๊นŒ?
    • ํ•˜๋‚˜์˜ ํŽ˜์ด์ง€๋กœ ๊ตฌ์„ฑ๋œ ์›น์•ฑ์ด๋ฏ€๋กœ ์„œ๋ฒ„์— 1ํšŒ ๋ฆฌ์†Œ์Šค๋ฅผ ์š”์ฒญํ•˜๊ณ (index.htmlํ•˜๋‚˜๋งŒ ์กด์žฌ)
    • ๊ทธ ์ดํ›„์—๋Š” ํ•„์š”ํ•  ๋–„, ๋ฐ์ดํ„ฐ๋งŒ ๋ฐ›์•„์™€์„œ ๊ธฐ์กด ํŽ˜์ด์ง€๋ฅผ ‘์ˆ˜์ •’ํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ํ™”๋ฉด์„ ์ˆ˜์ •ํ•จ. (re-rendering์ด ํ•„์š”ํ•˜์ง€ ์•Š์Œ)
    • ์‚ฌ์šฉ์ž์—๊ฒŒ ์ตœ์ ์˜ UX ๊ฒฝํ—˜ ์ œ๊ณต
  • ์•ˆ์ข‹์€ ์ ์€? SEO
    • SEO(Search Engine Optimization)์„ ์œ„ํ•ด์„œ๋Š” HTML ํŽ˜์ด์ง€ ์ „์ฒด๊ฐ€ ํ•„์š”ํ•œ๋ฐ, SPA ํŽ˜์ด์ง€์˜ HTML ํŒŒ์ผ์€ index.html ํ•˜๋‚˜๋ฟ์ด๊ธฐ ๋•Œ๋ฌธ์— ์„œ์น˜ ์—”์ง„์ด ์ฐพ์„ ์ˆ˜๊ฐ€ ์—†์–ด์„œ ๊ฒ€์ƒ‰์— ์ž˜ ์•ˆ๊ฑธ๋ฆฐ๋‹ค. ( → next.js๋กœ ๋ณด์™„ )

 

๋ฆฌ์•กํŠธ ํ”„๋กœ์ ํŠธ ๋งŒ๋“ค๊ธฐ

  1. npx create-react-app [๋งŒ๋“ค ํ”„๋กœ์ ํŠธ ์ด๋ฆ„]
  2. cd [ํ”„๋กœ์ ํŠธ ์œ„์น˜]
  3. yarn start

 

์ปดํฌ๋„ŒํŠธ๋ž€?

= ๋ฒฝ๋Œ? ๋ฒฝ๋Œ์„ ์Œ“์•„ ์ง‘์„ ์ง“๋Š”๋‹ค. ์ข‹์•„์š”.. ๋Œ“๊ธ€.. ๋“ฑ๋“ฑ ๋ชจ๋‘๊ฐ€ ํ•˜๋‚˜์˜ ๋ฒฝ๋Œ

  • SPA๋Š” ์ปดํฌ๋„ŒํŠธ ๋‹จ์œ„๋กœ ๋ณ€๊ฒฝ์‚ฌํ•ญ์„ ๋ฐ˜์˜ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๊นœ๋นก์ž„(๋ Œ๋”๋ง)์ด ์—†์Œ
  • ์ปดํฌ๋„ŒํŠธ๋ฅผ ํ†ตํ•ด UI๋ฅผ ์žฌ์‚ฌ์šฉ๊ฐ€๋Šฅํ•œ ๊ฐœ๋ณ„์ ์ธ ์—ฌ๋Ÿฌ ์กฐ๊ฐ์œผ๋กœ ๋‚˜๋ˆˆ๋‹ค. JavaScript์˜ ํ•จ์ˆ˜์™€ ์œ ์‚ฌํ•จ.
  • ์ปดํฌ๋„ŒํŠธ๋ฅผ ๋งŒ๋“ค์–ด๋ผ! ⇒ html์„ returnํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ค์–ด๋ผ!
  • prop์„ Input์œผ๋กœ๋ฐ›๊ณ  react Element๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

 

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ, jsx(html)์˜์—ญ ์ดํ•ดํ•˜๊ธฐ

import React from "react";
function App() {
  // <---- ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์˜์—ญ ---->
  const clicked = () => {
    alert("ํด๋ฆญ");
  };

  return (
    /* <---- HTML/JSX ์˜์—ญ  ---->*/
    <div
      style={{ //์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋ฅผ ์“ฐ๊ธฐ์œ„ํ•œ ๋Œ€๊ด„ํ˜ธ ์•ˆ์—, ๊ฐ์ฒด๋ฅผ ์“ฐ๊ธฐ ์œ„ํ•œ ๋Œ€๊ด„ํ˜ธ
        height: "100vh",
        display: " flex",
        flexDirection: "column",
        justifyContent: "center",
        alignItems: "center",
      }}
    >
      ์ด๊ฒƒ์€ ๋‚ด๊ฐ€ ๋งŒ๋“  ์ปดํฌ๋„ŒํŠธ ์ž…๋‹ˆ๋‹ค.
      <button id="btn" onClick={clicked}>
        ํด๋ฆญ
      </button>
    </div>
  );
}

export default App;

 

 

ํšŒ๊ณ 

์˜ค๋Š˜๋กœ ๋ฆฌ์•กํŠธ๋ฅผ ์‹œ์ž‘ํ–ˆ๋‹ค.

JSX ๋ฌธ๋ฒ•์ด๋‚˜ ๋ฆฌ์•กํŠธ ํ™˜๊ฒฝ์ด ์•„์ง ๋‚ฏ์„ค์–ด์„œ ๋งŽ์ด ํ—ท๊ฐˆ๋ฆฐ๋‹ค.

๋ฆฌ์•กํŠธ๋„ค์ดํ‹ฐ๋ธŒ๋กœ ๋ญ”์ง€๋„ ๋ชจ๋ฅด๊ณ  ์•ฑ๋งŒ๋“ค๋•Œ 

์ฝ”๋“œ์— ๊ฐ‘์ž๊ธฐ ํ™”์‚ดํ‘œ๊ฐ€ ์™œ๋‚˜์˜ค๋Š”์ง€, ๋Œ€๊ด„ํ˜ธ๋Š” ๋˜ ์™œ ๋‘๋ฒˆ์“ฐ๋Š”์ง€ 

์ „ํ˜€ ๋ชจ๋ฅธ์ฑ„๋กœ ๋งˆ๊ตฌ์žก์ด๋กœ ์ฝ”๋”ฉํ–ˆ๋˜ ์‹œ์ ˆ์ด ์ƒ๊ฐ๋‚˜๋ฉด์„œ

์•„ ์ด๊ฒŒ ๊ทธ๊ฑฐ์˜€๊ตฌ๋‚˜ ํ•˜๊ณ  ์ƒ๊ฐ์ด ๋‚ฌ๋‹ค.

๊ฐœ์ธ๊ณผ์ œ๋Š” ๋˜ ์–ด๋–ป๊ฒŒ ํ• ์ง€ ๋ง‰๋ง‰ํ•˜์ง€๋งŒ ์ฐจ๊ทผ์ฐจ๊ทผ ํ•ด๋ด์•ผ๊ฒ ๋‹ค.

'Today I Learned' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

2024.01.19 TIL  (0) 2024.01.19
2024.01.18 TIL  (0) 2024.01.18
2024.01.16 TIL  (0) 2024.01.16
2024.01.15 TIL  (0) 2024.01.15
2024.01.12 TIL  (1) 2024.01.12