๐Ÿ“— React

[React] React ์˜ ํŠน์ง•, JSX ์ดํ•ดํ•˜๊ธฐ

Tamii 2021. 4. 11. 17:32
๋ฐ˜์‘ํ˜•

CSR  ํด๋ผ์ด์–ธํŠธ ์‚ฌ์ด๋“œ ๋ Œ๋”๋ง

data(json)๊ฐ€์ ธ์™€ ํด๋ผ์ด์–ธํŠธ์—์„œ ๋ Œ๋”๋ง ํ•˜์—ฌ ๊ฒฐ๊ณผ ๋ณด์—ฌ์คŒ 

json๋ฐ์ดํ„ฐ๋งŒ ๋ฐ›์•„์™€์„œ ํด๋ผ์ด์–ธํŠธ ๋ Œ๋”๋ง ํ•˜์—ฌ๋ผ 

SSR ์„œ๋ฒ„ ์‚ฌ์ด๋“œ๋ Œ๋”๋ง 

๋ˆ„๋ฅด๋ฉด ํŽ˜์ด์ง€ ์ƒˆ๋กœ๊ณ ์นจ ( ํŽ˜์ด์ง€ ์ด๋™)

html ํŒŒ์ผ์„ ๊ฐ€์ ธ์™€์„œ ์„œ๋ฒ„์—์„œ ์ƒˆ๋กœ ๋ Œ๋”๋ง

 

 

 

 

๐Ÿ“Œ SPA ( Single Page Application )

ํ•œ single page์—์„œ ๋ฐ์ดํ„ฐ ๋ณ€ํ™”๊ฐ€ ๋‹ค ์ด๋ฃจ์–ด์ง€๋„๋ก ๋งŒ๋“  ํŽ˜์ด์ง€

์ƒˆ๋กœ๊ณ ์นจ ์—†์ด ๋น ๋ฅด๊ฒŒ ๋Œ€์‘ํ•˜๋„๋ก ๋งŒ๋“ฆ

๋ฐฑ์—”๋“œ API ์ œ๊ณต -> ํด๋ผ์ด์–ธํŠธ template ์ž‘์—… -> ๋ Œ๋”๋ง 

 

ํด๋ผ์ด์–ธํŠธ์—์„œ data๋“ค์„ ํŒŒ์‹ฑํ•˜๋ฉด์„œ JS๊ฐ€ ๋ณต์žกํ•ด์กŒ๋‹ค.

๋งŽ์€ ๋ชจ๋“ˆ๋“ค์„ ์–ด๋–ป๊ฒŒ ๊ฐ์ฒด์ง€ํ–ฅํ˜•์œผ๋กœ ๋งŒ๋“ค๊นŒ?

๊ทธ ์ค‘์— ๋น„๋™๊ธฐ ์ด๋ฒคํŠธ ์ฒ˜๋ฆฌ๋„  ์™„๋ฃŒํ•ด์•ผ ํ•จ

 

SPA๋ฅผ ์œ„ํ•ด ํ•„์š”ํ•œ ๊ธฐ์ˆ 

๋ผ์šฐํŒ… 

ํด๋ผ์ด์–ธํŠธ ์‚ฌ์ด๋“œ ๋ Œ๋”๋ง

์ƒํƒœ๊ด€๋ฆฌ 

view-model ๋ฐ”์ธ๋”ฉ

component ๋‹จ์œ„ ๊ฐœ๋ฐœ

 

SPA ํ”„๋ ˆ์ž„์›Œํฌ

Angular, React, Vue.js ...

 

๐Ÿ“Œ MVC ( Modle View Controller ) ํŒจํ„ด

Action -> Controller -> Model -> View

์ถœ์ฒ˜: XESCHOOL / Facebook

 

View ์— Model์„ ์–ธ

Model ๋ณ€๊ฒฝ -> Viwe ๋ Œ๋”๋ง ์•Œ์•„์„œ 

DOM ์— access ์“ธํ•„์š” ์—†์Œ ( DOM  ์ง์ ‘ ์ ‘๊ทผ ํ•„์š”๊ฐ€ ์—†์Œ)

 

 

๐Ÿ“Œ Flux ํŒจํ„ด

ํ•œ  ์ชฝ ๋ฐฉํ–ฅ์œผ๋กœ ํ๋ฅด๋Š” ํŽ˜์ด์ง€ ๊ตฌ์กฐ

์ถœ์ฒ˜: facebook

๋ฐ์ดํ„ฐ์˜ ๊ตฌ์กฐ : top-down 

๋ฐ์ดํ„ฐ ๋ณ€๊ฒฝ ---> ๋ Œ๋”๋ง

์žฆ์€ ๋ Œ๋”๋ง์˜ ๋‹จ์ ์ด ์žˆ์Œ

 


๐Ÿ“Œ React

UI๋ฅผ ๋งŒ๋“œ๋Š” JS ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ (ํ•จ์ˆ˜์˜ ์ง‘ํ•ฉ )

component์˜ ๋ชจ์ž„์œผ๋กœ ์ด๋ฃจ์–ด์ง

๋ณ€๊ฒฝ์‚ฌํ•ญ์„ ํƒ์ง€ํ•˜์—ฌ  View ๋ Œ๋”๋ง ์ตœ์†Œํ™”

๋ฐ์ดํ„ฐ๋ฅผ ์„ ์–ธํ•ด๋‘๋ฉด ๋ฐ์ดํ„ฐ ๋ณ€๊ฒฝ์‹œ ์ž๋™ ๋ Œ๋”๋ง

 

virtual DOM์„ ๋งŒ๋“ค์–ด ๋ณ€๊ฒฝ์‚ฌํ•ญ์„ ๊ณ„์† ๋น„๊ต 

Browser DOM ์—์„œ๋Š” DOM ๋ณ€๊ฒฝ ์ตœ์†Œํ™” ํ•˜์—ฌ ๋ฐ˜์˜

 

๊ธฐ์กด์˜ ํ”„๋ ˆ์ž„์›Œํฌ React
ํŠน์ • ๊ฐ’์ด ๋ฐ”๋€Œ๋ฉด ํŠน์ • DOM์˜ ์†์„ฑ์ด ๋ฐ”๋€Œ๋„๋ก ์—ฐ๊ฒฐ ํŠน์ • ๊ฐ’์ด๋ฐ”๋€Œ๋ฉด ์ƒˆ๋กœ ๋งŒ๋“ค์–ด์„œ ๋ณด์—ฌ์คŒ

์—…๋ฐ์ดํŠธ๋ฅผ ์–ด๋–ป๊ฒŒ ํ•ด์•ผํ•  ์ง€ ๊ณ ๋ฏผํ•˜์ง€ ์•Š์•„๋„ ๋จ 

ํ•˜์ง€๋งŒ ๋ชจ๋“ ๊ฑธ ๋‚ ๋ฆฌ๊ณ  ์ƒˆ๋กœ ๋ณด์—ฌ์ฃผ๋ฉด ๋„ˆ๋ฌด ๋Š๋ฆฌ๋‹ˆ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด ๋‚˜์˜จ ๋…€์„์ด virtual DOM

 

โš ๏ธ Virtual DOM ์ด๋ž€?

๋ฐ์ดํ„ฐ ๋ณ€๊ฒฝ์„ ๋น„๊ตํ•˜๊ธฐ ์œ„ํ•œ ๊ฐ€์ƒ DOM 

DOM ๋ณ€๊ฒฝ์„ ์ตœ์†Œํ™”ํ•˜์—ฌ ๋ Œ๋”๋ง  ํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•จ

๋‹จ์ˆœ JS ๊ฐ์ฒด์ด๊ธฐ ๋•Œ๋ฌธ์— ์‹ค์ œ ๋ธŒ๋ผ์šฐ์ €์˜ DOM  ๋ณด์—ฌ์ฃผ๋Š” ๊ฒƒ๋ณด๋‹ค ์†๋„๊ฐ€ ํ›จ์”ฌ ๋น ๋ฆ„

 

 

โ– ๋™์ž‘ ๋ฐฉ์‹

1) ๋ฐ์ดํ„ฐ ์—…๋ฐ์ดํŠธ --> ์ „์ฒด UI ๋ฅผ virtual DOM ์— ๋ฆฌ๋ Œ๋”๋ง

2) ์ด์ „ virtual DOM๊ณผ ํ˜„์žฌ virtual DOM ๋น„๊ต (diff)

3) ๋ฐ”๋€ ๋ถ€๋ถ„๋งŒ real DOM ์— ์ ์šฉ

 

React๋Š” virtual  DOM์„ ํ†ตํ•ด ๋ Œ๋”๋ง์„ ํ•œ๋ฒˆ์œผ๋กœ ์ค„์—ฌ ํšจ์œจ์ ์œผ๋กœ HTML์„ ์ œ์–ดํ•˜๊ฒŒ ๋จ

๋ฐ์ดํ„ฐ์˜ ํ๋ฆ„๊ณผ ๊ตฌ์„ฑ์„ ์ผ๊ด€๋˜๊ฒŒ ๋งŒ๋“ค์–ด์คŒ

 

 

 

 


์ฝ”๋“œ๋กœ ์•Œ์•„๋ณด๊ธฐ

react = component ๊ฐœ๋ฐœ๋ฐฉ์‹

 

 

๊ฒฐ๊ตญ Single page์—๋Š” HTML ์ฝ”๋“œ๊ฐ€ ์—†์Œ  -> ๋ชจ๋“  HTML ์ฝ”๋“œ๋Š” JS์— ์žˆ์Œ

< html > 

<!DOCTYPE html>
<html lang="en">

<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
	<meta name="theme-color" content="#000000">
	<title>React App</title>
</head>

<body>
	<div id="root"></div>
</body>

</html>

< index.js >

import { StrictMode } from "react";
import ReactDOM from "react-dom";

import App from "./App";

const rootElement = document.getElementById("root");
ReactDOM.render(
  <StrictMode>
    <App />
  </StrictMode>,
  rootElement
);

index.js๊ฐ€ entry point ๋กœ ์„ค์ •๋˜๊ณ  ๋‹ค๋ฅธ ๋งŽ์€ component๋“ค์„ ๋ถˆ๋Ÿฌ์™€์„œ ์‚ฌ์šฉ

ReactDOM>render : ์‹ค์ œ DOM ๋‚ด๋ถ€์— ๋ฆฌ์•กํŠธ ์ปดํฌ๋„ŒํŠธ๋ฅผ ๋ Œ๋”๋ง 

ex) ์œ„ ์˜ˆ์‹œ : id๊ฐ€ "root"์ธ DOM์„ ์„ ํƒํ•˜์—ฌ ๋ Œ๋”๋งํ•˜๊ฒ ๋‹ค.

 

 

๐Ÿ“Œ JSX

์ฐธ๊ณ  : (2021.03.16 - [๐Ÿ“˜ React] - [ React ] JSX ์ดํ•ดํ•˜๊ธฐ & Proptypes

* React์—์„œ ์‚ฌ์šฉํ•˜๋Š” ํ‘œ์ค€ html ๋ฌธ๋ฒ•  (์‹ค์ œ๋กœ๋Š” JavaScript) 

* DOM API๋ฅผ ์ง์ ‘ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ ๋„ DOM์„ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์ž‡๊ฒŒ ํ•จ 

* babel์„ ํ†ตํ•ด transpiling ์„ ํ•ด์•ผ ํ•จ

 render() {
    return (
      <div>์•ˆ๋…•ํ•˜์„ธ์š”</div>
    )
  }

 


โ˜ป JSX ๊ทœ์น™

1. ํƒœ๊ทธ๋Š” ๊ผญ ๋‹ซํ˜€์žˆ์–ด์•ผํ•จ

<div></div>

๋‹จ ํƒœ๊ทธ ์‚ฌ์ด์— ๋‚ด์šฉ์ด ๋“ค์–ด๊ฐ€์ง€ ์•Š์„ ๋•Œ๋Š” Self Closing ํƒœ๊ทธ ๊ฐ€๋Šฅ

<Hello />

2. 2๊ฐœ ์ด์ƒ์˜ ํƒœ๊ทธ๋Š” ๋ฌด์กฐ๊ฑด ํ•˜๋‚˜์˜ ํƒœ๊ทธ๋กœ ๊ฐ์‹ธ์•ผ ํ•จ

//์˜ค๋ฅ˜
return (
    <Hello />
    <div>์•ˆ๋…•</div>
);

//OK
 return (
 	<div>
    	<Hellog />
    	<div>์•ˆ๋…•</div>
    </div>
);

๋ณต์žกํ•  ๊ฒฝ์šฐ Fragment ์‚ฌ์šฉ ๊ฐ€๋Šฅ

๋ธŒ๋ผ์šฐ์ € ์ƒ์—์„œ ๋ณ„๋„์˜ element๋กœ ๋‚˜ํƒ€๋‚˜์ง€ ์•Š์Œ

 return (
 	<>
	<Hellog />
	<div>์•ˆ๋…•</div>
	</>
);

3. JSX ์•ˆ์— JS ๊ฐ’ ์‚ฌ์šฉ ์‹œ {} 

 return (
 	<>
	<Hellog />
	<div>{name}</div>
	</>
);

4. class ์„ค์ • ์‹œ className (o) class(x)

 return (
    <>
      <Hello />
      <div>{name}</div>
      <div className="boxTitle"></div>
    </>
  );

5. ์ฃผ์„์€ {/* */}

 return (
    <>
      {/* ์ฃผ์„์ž…๋‹ˆ๋‹ค */}
      <Hello />
      <div>{name}</div>
      <div className="boxTitle"></div>
    </>
  );

 

6. ์‚ฌ์šฉ์ž ์ง€์ • component ์‚ฌ์šฉ

render() {
    return (
      <div>
      <div>์•ˆ๋…•ํ•˜์„ธ์š”</div>
      <MyList  info="firstPost"></MyList>
      </div>
    )
  }

<MyList > : ์‹ค์ œ ์กด์žฌํ•˜๋Š” HTML tag (x) ์‚ฌ์šฉ์ž Component (o)

new MyList({info: "firstPost"});

JS๋กœ ํ•˜๋ฉด ์œ„์™€ ๊ฐ™๋‹ค๊ณ  ํ•  ์ˆ˜ ์žˆ์Œ