JiwonDev

React #1 ๊ฐœ๋…

by JiwonDev

HTML, CSS, JS

2021.03.22 - [Web/HTML&CSS] - HTML, CSS ๊ธฐ์ดˆ

 

ECMA Script

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์˜ ํ‘œ์ค€. Java ์ด๋ฆ„์˜ ์ €์ž‘๊ถŒ ๋ฌธ์ œ + ๋ฌธ๋ฒ• ํ‘œ์ค€์ด๋ผ๋Š” ์˜๋ฏธ๋กœ  ECMA(European Computer Manafactures Assocation)๋ผ๊ณ  ๋ถ€๋ฆ…๋‹ˆ๋‹ค. 2015๋…„ ES6 ์ด์ „์˜ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋Š” ์–ธ์–ด๋ผ๊ณ  ๋ถ€๋ฅด๊ธฐ์—” ๋ฏผ๋งํ• ์ •๋„๋กœ ๊ฐœํŒ์ด์—ˆ๊ณ  ES6 (ES2015) ์ดํ›„ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ํ‘œ์ค€์ด ํ™•๋ฆฝ๋˜๋ฉฐ ์—ฌ๋Ÿฌ ํŽธ๋ฆฌํ•œ ๊ธฐ๋Šฅ์ด ์ถ”๊ฐ€๋˜๊ธฐ ์‹œ์ž‘ํ–ˆ์Šต๋‹ˆ๋‹ค.

 

Node.js

์›น ๋ธŒ๋ผ์šฐ์ €๊ฐ€ ์•„๋‹Œ ๊ณณ์—๋„ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๊ฐ€ ์ž‘๋™ํ•˜๋„๋ก ํ•ด์ฃผ๋Š” ๋Ÿฐํƒ€์ž„ ํ™˜๊ฒฝ.

์œ ๋ช…ํ•œ ํŒจํ‚ค์ง€ ๋‹ค์šด๋กœ๋“œ ๋งค๋‹ˆ์ €(npm)์„ ๊ธฐ๋ณธ ์ œ๊ณตํ•ด์ฃผ๋ฉฐ ํฌ๋กฌ์˜ V8 ์›น ๋ธŒ๋ผ์šฐ์ € ์—”์ง„์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

 

Npm, Yarn

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ํŒจํ‚ค์ง€ ๋งค๋‹ˆ์ € ํ”„๋กœ๊ทธ๋žจ์ž…๋‹ˆ๋‹ค. ๊ฐ์ข… ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์˜ ๋ฒ„์ „, ์ข…์†์„ฑ์„ ๊ด€๋ฆฌํ•ด์ฃผ๋ฉฐ ๋ณต์žกํ•˜๊ฒŒ ์—ฎ์–ด์žˆ๋Š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋“ค์„ ์ฝ˜์†” ๋ช…๋ น์–ด ํ•œ๋ฒˆ์œผ๋กœ ์‰ฝ๊ฒŒ ๋‹ค์šด๋กœ๋“œ, ๊ด€๋ฆฌ ํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋„์™€์ค๋‹ˆ๋‹ค. Node.js ์—์„œ ์ œ๊ณตํ•ด์ฃผ๋Š”๊ฒŒ npm(node package manager)์ด๊ณ , ํŽ˜์ด์Šค๋ถ์—์„œ npm์˜ ๋ณด์•ˆ์„ฑ์ด๋‚˜ ์†๋„๋ฅผ ๊ฐœ์„ ํ•˜๋ ค๊ณ  ๋งŒ๋“ ๊ฒŒ yarn ํŒจํ‚ค์ง€ ๋งค๋‹ˆ์ €์ž…๋‹ˆ๋‹ค. ๋‘˜ ์ค‘ ๋ญ๊ฐ€ ์ข‹๋‹ค๊ธฐ๋ณด๋‹ค๋Š”, ๊ฐ๊ฐ์˜ ์žฅ๋‹จ์ ์ด ์žˆ์Šต๋‹ˆ๋‹ค.

ํŽ˜์ด์Šค๋ถ ๊ณต์‹ํŽ˜์ด์ง€ Yarn ์†Œ๊ฐœ๊ธ€(2016)

์ฐธ๊ณ ๋กœ ์•„๋ž˜์— ์žˆ๋Š” Babel, React๋„ ์ด๋Ÿฌํ•œ ํŒจํ‚ค์ง€ ๋งค๋‹ˆ์ €๋ฅผ ์ด์šฉํ•˜์—ฌ ์†์‰ฝ๊ฒŒ ์ตœ์‹ ๋ฒ„์ „์„ ๋‹ค์šด ๋ฐ›์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

Babel

์ตœ์‹  ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์ฝ”๋“œ ๋ฌธ๋ฒ•์„ ์‚ฌ์šฉํ•˜๋”๋ผ๋„ ๊ฐ ๋ธŒ๋ผ์šฐ์ €์— ๋งž๋Š” ๋ฒ„์ „์œผ๋กœ ๋ณ€ํ™˜ํ•ด์ฃผ๋Š” ํฌ๋กœ์Šค ์ปดํŒŒ์ผ๋Ÿฌ

๋ฆฌ์•กํŠธ์˜ ์‚ฌ์šฉ์ž๊ฐ€ ๋Š˜์–ด๋‚˜ ์ด์ œ๋Š” ๋ฆฌ์•กํŠธ์—์„œ ์‚ฌ์šฉํ•˜๋Š” JSX ๋ฌธ๋ฒ•๋„ ํ‘œ์ค€์œผ๋กœ ์ง€์›ํ•ฉ๋‹ˆ๋‹ค.

 

ํ”„๋กœ์ ํŠธ ์ข…์†์„ฑ ๊ด€๋ฆฌ

์„œ๋กœ์˜ ์Šค์ฝ”ํ”„(๋ณ€์ˆ˜์ด๋ฆ„, ๋ฉ”๋ชจ๋ฆฌ) ์นจ๋ฒ”, ์—ฌ๋Ÿฌ๊ฐœ์˜ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋‚˜ ํŒŒ์ผ๋“ค์˜ ๋ฒ„์ „ ํ˜ธํ™˜๊ณผ import ์ค‘๋ณตํ˜ธ์ถœ, ๋ˆ„๋ฝ๋“ฑ์„ ๋ฐฉ์ง€ํ•˜๋Š” ๊ฑธ ์ข…์†์„ฑ ๊ด€๋ฆฌ๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ES6 ์ดํ›„ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์˜ ํ‘œ์ค€์ด ๋งŒ๋“ค์–ด์ง€๊ณ  ํ•œ ์›นํŽ˜์ด์ง€์— ์—ฌ๋Ÿฌ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์ฝ”๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” SPA(Single Page App) ๊ฐœ๋ฐœ ๋ฐฉ์‹์ด ํฅํ•˜๊ฒŒ ๋˜๋ฉด์„œ ํ”„๋กœ์ ํŠธ ์ข…์†์„ฑ์„ ๊ด€๋ฆฌํ•˜๊ธฐ๊ฐ€ ์–ด๋ ค์›Œ์กŒ์Šต๋‹ˆ๋‹ค.

 

 

Webpack

์œ„์˜ ์ข…์†์„ฑ์„ ์‰ฝ๊ฒŒ ๊ด€๋ฆฌํ•˜๊ธฐ์œ„ํ•ด ์‚ฌ์šฉํ•˜๋Š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์ž…๋‹ˆ๋‹ค.

2021๋…„ ๊ธฐ์ค€ ๊ฐ€์žฅ ์„ฑ๋Šฅ์ด ์ข‹์€ ์ข…์†์„ฑ ๊ด€๋ฆฌ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๋ชจ๋“ˆ ๋ฒˆ๋“ค๋Ÿฌ (Static Module Bundler)๋กœ ๊ฐœ๋ฐœ์ž๊ฐ€ ์ž‘์„ฑํ•œ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์ฝ”๋“œ๋ฅผ ์ „๋ถ€ ์ฝ์–ด์„œ ๊ด€๋ฆฌํ•˜๊ธฐ ํŽธํ•œ ํ˜•ํƒœ๋กœ ๋ณ€ํ™˜์‹œ์ผœ์ค๋‹ˆ๋‹ค. ๋‹ค์–‘ํ•œ ๋ถ€๊ฐ€๊ธฐ๋Šฅ์ด ์žˆ์œผ๋ฉฐ webpack์„ ์ด์šฉํ•˜์—ฌ ๋ฆฌ์•กํŠธ์—์„œ ์‚ฌ์šฉํ•œ JSX๋ฌธ๋ฒ•์„ ๊ฐ„๋‹จํ•˜๊ฒŒ babel์œผ ์—ฐ๊ฒฐ์‹œ์ผœ ์•Œ๋งž์€ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์ฝ”๋“œ๋กœ ๋ณ€ํ™˜ ์‹œํ‚ฌ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

2021.04.16 - [Web/React] - Webpack

์ฐธ๊ณ ๋กœ Webpack์ด ๋‚˜์˜ค๊ธฐ ์ „์—๋Š” (~2014) Gulp๋‚˜ Grunt ๊ฐ™์€ ๋ฆฌ์†Œ์Šค ๊ด€๋ฆฌ ํˆด๋งŒ ์‚ฌ์šฉํ–ˆ์Šต๋‹ˆ๋‹ค.

 

React

ํŽ˜์ด์Šค๋ถ์—์„œ ๊ฐœ๋ฐœํ•œ ์ปดํฌ๋„ŒํŠธ๊ธฐ๋ฐ˜ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ View ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ

HTML๊ณผ JS๋ฅผ ํ•จ๊ป˜ ์‚ฌ์šฉํ•˜๋Š” JSX๋ฌธ๋ฒ•์„ ์‚ฌ์šฉ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. (Babel์„ ํ†ตํ•ด ๋ฆฌ์•กํŠธ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” JS๋กœ ๋ณ€ํ™˜๋จ) ๋ชจ๋“  ๊ฒƒ์€ ์ปดํฌ๋„ŒํŠธ( ๊ฐ์ฒด )๋กœ ์ทจ๊ธ‰๋˜๋ฉฐ ๊ฐ€์ƒDOM์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

 

React์™€ ํ•จ๊ป˜ ๋งŽ์ด ์‚ฌ์šฉํ•˜๋Š” ํ”„๋ ˆ์ž„์›Œํฌ๋กœ๋Š” express(HTML, Connect Component ๋„๊ตฌ) , NextJS(React SSR ๋„๊ตฌ) ๋“ฑ์ด์žˆ์Šต๋‹ˆ๋‹ค.  2021๋…„ ๊ธฐ์ค€ ์›น์—์„œ ๊ฐ€์žฅ ํ•ซํ•œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์ค‘ ํ•˜๋‚˜์ž…๋‹ˆ๋‹ค.

 

* [๋”๋ณด๊ธฐ] Chrome ๊ฐœ๋ฐœ์ž๋„๊ตฌ

๋”๋ณด๊ธฐ

React-dectector (๋ฆฌ์•กํŠธ ์‚ฌ์šฉ์—ฌ๋ถ€)

chrome.google.com/webstore/detail/react-detector/jaaklebbenondhkanegppccanebkdjlh?hl=en-US

 

Show-me-te-react(๋ฆฌ์•กํŠธ ์ปดํฌ๋„ŒํŠธ๋ฅผ ํ™”๋ฉด์— ์ƒ‰์น ํ•ด์„œ ํ‘œ์‹œ)

chrome.google.com/webstore/detail/show-me-the-react/iaebolhfcmodobkanmaahdhnlplncbnd?hl=en-US

 

React-Developer Tools(ํฌ๋กฌ ๊ฐœ๋ฐœ์ž๋„๊ตฌ์— React ํƒญ ์ถ”๊ฐ€)

chrome.google.com/webstore/detail/react-developer-tools/fmkadmapgofadopljbjfkapdkoienihi?hl=en-US

React-dectector (๋ฆฌ์•กํŠธ ์‚ฌ์šฉ์—ฌ๋ถ€)

chrome.google.com/webstore/detail/react-detector/jaaklebbenondhkanegppccanebkdjlh?hl=en-US

 

Show-me-te-react(๋ฆฌ์•กํŠธ ์ปดํฌ๋„ŒํŠธ๋ฅผ ํ™”๋ฉด์— ์ƒ‰์น ํ•ด์„œ ํ‘œ์‹œ)

chrome.google.com/webstore/detail/show-me-the-react/iaebolhfcmodobkanmaahdhnlplncbnd?hl=en-US

 

React-Developer Tools(ํฌ๋กฌ ๊ฐœ๋ฐœ์ž๋„๊ตฌ์— React ํƒญ ์ถ”๊ฐ€)

chrome.google.com/webstore/detail/react-developer-tools/fmkadmapgofadopljbjfkapdkoienihi?hl=en-US

 

Virtual DOM ( ๊ฐ€์ƒ DOM )

๋ฆฌ์•กํŠธ๋Š” ์ปดํฌ๋„ŒํŠธ ๊ธฐ๋ฐ˜์œผ๋กœ ๊ฐ€์ƒ DOM์„ ์‚ฌ์šฉํ•˜๋Š” ๋Œ€ํ‘œ์ ์ธ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์ค‘ ํ•˜๋‚˜์ž…๋‹ˆ๋‹ค.

๋ฆฌ์•กํŠธ๋Š” View๋งŒ ๋‹ค๋ฃจ๋Š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์ด๋ฉฐ SPA(Single Page App)์— ์‚ฌ์šฉํ•˜๊ธฐ ์ข‹์Šต๋‹ˆ๋‹ค.

2021.03.29 - [Web/React] - ๋ฆฌ์•กํŠธ์™€ Virutal DOM

 

JSX ๋ฌธ๋ฒ•

import React from 'react';

const MyComponents = {
  DatePicker: function DatePicker(props) {
    return (
    	<div>
          Imagine a {props.color} datepicker here.
        </div>
    );
  }
}

function BlueDatePicker() {
  return <MyComponents.DatePicker color="blue" />;
}

๋ฆฌ์•กํŠธ์—์„œ ์‚ฌ์šฉํ•˜๋Š” ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ํ™•์žฅ ๋ฌธ๋ฒ• (JavaScript eXtension ๋˜๋Š” JS XML)์ž…๋‹ˆ๋‹ค.

HTML์„ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์ฝ”๋“œ๋‚ด์—์„œ ์‚ฌ์šฉ ํ•  ์ˆ˜ ์žˆ๊ณ  ์ฝ”๋“œ๋ฅผ ์ง๊ด€์ ์œผ๋กœ ์“ฐ๊ธฐ ํŽธํ•˜๊ฒŒ ๋งŒ๋“ค์–ด์ค๋‹ˆ๋‹ค. ๋‹ค๋งŒ, ๊ฒฐ๊ตญ์—๋Š” JSX๋ฌธ๋ฒ•๋„ Babel์„ ์ด์šฉํ•˜์—ฌ ๋ฐ”๋‹๋ผ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์ฝ”๋“œ๋กœ ๋ณ€ํ™˜๋˜๋ฏ€๋กœ, JSX ์ฝ”๋“œ๋Š” ์ˆœ์ˆ˜ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋กœ ๋Œ€์ฒด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.

import react

// JSX ๋ฌธ๋ฒ•
var a =(
    <div>
        ๋ฐ˜๊ฐ‘์Šต๋‹ˆ๋‹ค <b> React์— ์˜ค์‹ ๊ฒƒ์„ ํ™˜์˜ํ•ฉ๋‹ˆ๋‹ค. </b>
    </div>
);

ํ•ด๋‹น ์ฝ”๋“œ๋Š” Babel์— ์˜ํ•ด ์•„๋ž˜์™€ ๊ฐ™์€ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์ฝ”๋“œ๋กœ ๋ณ€ํ™˜๋ฉ๋‹ˆ๋‹ค.

// ๋ฐ”๋‹๋ผ JS
"use strict":
import react
 
var a = React.createElement(
    "div",
    null,
    "๋ฐ˜๊ฐ‘์Šต๋‹ˆ๋‹ค",
    React.createElement(
        "b",
        null,
        "React์— ์˜ค์‹ ๊ฒƒ์„ ํ™˜์˜ํ•ฉ๋‹ˆ๋‹ค."
    )
);

 

์ˆœ์ˆ˜ ๋ฆฌ์•กํŠธ

React๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋Œ€๋ถ€๋ถ„ JSX๋ฌธ๋ฒ•์„ ์ด์šฉํ•˜์—ฌ ๊ฐ„๊ฒฐํ•˜๊ฒŒ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•ฉ๋‹ˆ๋‹ค.

JSX๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š”๋‹ค๋ฉด  ๋‚ด๋ถ€์ ์œผ๋กœ ์–ด๋–ค ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์ฝ”๋“œ๋กœ ๊ตฌ์„ฑ๋˜์–ด์žˆ๋Š”์ง€ ๊ฐ„๋‹จํ•˜๊ฒŒ ํ•œ๋ฒˆ ์‚ดํŽด ๋ด…์‹œ๋‹ค.

 

#1 import react

import React from 'react';
import ReactDOM from 'react-dom';

๊ธฐ์กด์˜ react๋Š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ํ•œ๊ฐœ๋งŒ ๋ถˆ๋Ÿฌ์˜ค๋ฉด ๋˜์—ˆ์œผ๋‚˜, ๋ฒ„์ „ 0.14 ์ดํ›„๋กœ React ์ฝ”๋“œ ๋ถ€๋ถ„๊ณผ React-Virutal-DOM ๋ถ€๋ถ„์ด ๋ถ„๋ฆฌ๋˜๊ฒŒ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ์ด๋Š” ๋‚˜์ค‘์— ๊ฐ™์€ ๋ฆฌ์•กํŠธ ์ฝ”๋“œ๋กœ ๋‹ค๋ฅธ ํ”Œ๋žซํผ๊นŒ์ง€ ํ™•์žฅํ•ด์„œ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•จ์ž…๋‹ˆ๋‹ค. (React-Dom์€ ์›น๊ฐœ๋ฐœ์šฉ, React-Native์€ ์•ฑ๊ฐœ๋ฐœ์šฉ)

 

  • ์ฐธ๊ณ ๋กœ JSX ๋ฌธ๋ฒ•์„ ์ด์šฉํ•˜์—ฌ cssํŒŒ์ผ์ด๋‚˜ ์™ธ๋ถ€ ์ด๋ฏธ์ง€๋กœ import๋กœ ๋ถˆ๋Ÿฌ์™€ ์‚ฌ์šฉ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
import logo from './logo.svg';
import './App.css';

 

  • ๋ฆฌ์•กํŠธ ์ปดํฌ๋„ŒํŠธ ๊ฐ์ฒด์˜ ๊ฒฝ์šฐ <Class/> ๋กœ ๋ถˆ๋Ÿฌ์™€์„œ ๋‹ค๋ฅธ ์ฝ”๋“œ์— ์‚ฌ์šฉ ํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค.
class Hello extends React.Component{
    render() {
        return (
            <div>
                ์•ˆ๋…•ํ•˜์„ธ์š”
            </div>
        );
    }
}
 
// ์ปดํฌ๋„ŒํŠธ ๊ฐ์ฒด๋Š” JSX ๋ฌธ๋ฒ• <Class/> ๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
// ๋ฌผ๋ก  HTML ํƒœ๊ทธ์ฒ˜๋Ÿผ <Class type="Hello> </Class> ์ด๋Ÿฐ ํ˜•ํƒœ๋กœ ์†์„ฑ ์ž…๋ ฅ๋„ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.
class App extends React.Component{
    render() {
        return(
            <Hello/> //  == class Hello {...}
        );
    }
}

 

 

#2. HTML element ์ƒ์„ฑ

// React.createElement("tag", {์†์„ฑ๊ฐ’, key:value }, "๋‚ด์šฉ")
React.createElement("h1", null, "์•ˆ๋…•ํ•˜์„ธ์š”")
React.createElement("div", {id: "myID", 'data-type':"title" }, "์ œ๋ชฉ์— ๋“ค์–ด๊ฐˆ ๋‚ด์šฉ์ž…๋‹ˆ๋‹ค");

์œ„์˜ ๋ฆฌ์•กํŠธ ์ฝ”๋“œ๋Š” ๋‹ค์Œ HTML ์ฝ”๋“œ์™€ ๊ฐ™์€ DOM์„ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.

<h1> ์•ˆ๋…•ํ•˜์„ธ์š” </h1>

<!-- ํ•ด๋‹น element๊ฐ€ ์ตœ์ƒ๋‹จ์ธ ๊ฒฝ์šฐ data-reactroot ํด๋ž˜์Šค๊ฐ€ ์ž๋™ ์ถ”๊ฐ€๋ฉ๋‹ˆ๋‹ค. -->
<div data-reactroot id="myID" data-type="title">
	์ œ๋ชฉ์— ๋“ค์–ด๊ฐˆ ๋‚ด์šฉ์ž…๋‹ˆ๋‹ค.
</div>

 

 

# 3. ์ž์‹, ํƒœ๊ทธ ๊ฐ์‹ธ๊ธฐ

  <ul class="myClass">
    <li> ๋ชฉ๋ก 1</li>
    ์•ˆ๋…•ํ•˜์„ธ์š”
    <li> ๋ชฉ๋ก 2</li>
    <li myType = "๋ฉ”๋กฑ" > ๋ชฉ๋ก 3</li>
    <li> ๋ชฉ๋ก 4</li>
  </ul>

 

  • ์ด๋ ‡๊ฒŒ ํƒœ๊ทธ๋ฅผ ๊ฐ์‹ธ๊ณ  ์‹ถ๋‹ค๋ฉด, ์•„๋ž˜ ์ฝ”๋“œ ์ฒ˜๋Ÿผ ์ž‘์„ฑํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.
    React.createElement( ... ) ์˜ 3๋ฒˆ์งธ ์ดํ›„ ์ธ์ž๋Š” ์ „๋ถ€ ์ž์‹(children, ๋‚ด์šฉ)์œผ๋กœ ์ทจ๊ธ‰๋ฉ๋‹ˆ๋‹ค. 
React.createElement(
  "ul", // ํƒœ๊ทธ๋ช…
  null, // ์†์„ฑ๊ฐ’, props
  React.createElement("li",null, "๋ชฉ๋ก1"),
  "์•ˆ๋…•ํ•˜์„ธ์š”",
  React.createElement("li",null, "๋ชฉ๋ก2"),
  React.createElement("li",{ myType:"๋ฉ”๋กฑ" }, "๋ชฉ๋ก3"),
  React.createElement("li",null, "๋ชฉ๋ก4"),
)

 

  • ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์ฝ”๋“œ๋กœ ์ƒ์„ฑ๋œ ๋ฆฌ์•กํŠธ ๊ฐ์ฒด(๋ถ€๋ชจ)๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์†์„ฑ๊ฐ’์„ ๊ฐ€์ง€๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.
    <div> ์—ฌ๊ธฐ์— ์ ์€ ๋‚ด์šฉ </div> ์€ ํ•ด๋‹น ๋ฆฌ์•กํŠธ ๊ฐ์ฒด์˜ props(์†์„ฑ๊ฐ’) ์•ˆ์— children ์†์„ฑ์œผ๋กœ ์ €์žฅ๋ฉ๋‹ˆ๋‹ค.
// ... React๋กœ ๋งŒ๋“  ๊ฐ์ฒด์˜ ์†์„ฑ๊ฐ’
{
"type":"ul",
  "props":{
    "children":[
      { "type:" : "li", "props": {"children" : "๋ชฉ๋ก1"} },
      "์•ˆ๋…•ํ•˜์„ธ์š”",
      { "type:" : "li", "props": {"children" : "๋ชฉ๋ก2"} },
      { "type:" : "li", "props": {"children" : "๋ชฉ๋ก3", "myType":"๋ฉ”๋กฑ"} },
      { "type:" : "li", "props": {"children" : "๋ชฉ๋ก4"} },
    ]
  }
}

 

 

# 4. ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ํ™œ์šฉํ•˜๊ธฐ

๋ฆฌ์•กํŠธ์˜ ๊ฐ€์žฅ ํฐ ์žฅ์ ์€ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋ฅผ ์ด์šฉํ•˜์—ฌ HTML ์—˜๋ฆฌ๋จผํŠธ์™€ ๋ฐ์ดํ„ฐ๋ฅผ ๋ถ„๋ฆฌ ํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ์ ์ž…๋‹ˆ๋‹ค.

์œ„์—์„œ ๋…ธ๊ฐ€๋‹ค๋กœ ๋งŒ๋“  ์—ฌ๋Ÿฌ๊ฐœ์˜ <ul> ๋ฆฌ์ŠคํŠธ๋Š”, ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ƒ์„ฑ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.

์ฐธ๊ณ ๋กœ HTML "class" ์†์„ฑ์€ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์˜ ํด๋ž˜์Šค ์ƒ์„ฑ ํ‚ค์›Œ๋“œ์™€ ๊ฒน์น˜๋ฏ€๋กœ ๋ฆฌ์•กํŠธ์—์„œ๋Š” "className"์œผ๋กœ ๋ฐ”๊ฟ” ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

const items = [ // ๋ฐ์ดํ„ฐ
  '๋ชฉ๋ก1',
  '๋ชฉ๋ก2',
  '๋ชฉ๋ก3',
  '๋ชฉ๋ก4',
]

React.createElement( // HTML - DOM ์ƒ์„ฑ
   "ul",
  {"className": "myList"}, // class : "myList"
  items.map( content => React.createElement("li", null, content))
)
  <ul class="myList">
    <li> ๋ชฉ๋ก 1</li>
    <li> ๋ชฉ๋ก 2</li>
    <li> ๋ชฉ๋ก 3</li>
    <li> ๋ชฉ๋ก 4</li>
  </ul>

 

# 5. ์ปดํฌ๋„ŒํŠธ๋กœ ๋งŒ๋“ค๊ธฐ

' ๋ฆฌ์•กํŠธ์˜ ๋ชจ๋“  ๊ฒƒ์€ ์ปดํฌ๋„ŒํŠธ๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ๋‹ค '

์œ„์—์„œ๋Š” ๋ฆฌ์•กํŠธ๋ฅผ ์ด์šฉํ•˜๋ฉฐ HTML ์š”์†Œ๋ฅผ ์ƒ์„ฑ๋งŒ ํ–ˆ์„ ๋ฟ, ์‹ค์ œ ๋ฌธ์„œ์— ๋ฐ˜์˜ํ•˜์ง€๋Š” ์•Š์•˜์Šต๋‹ˆ๋‹ค.

๋‚ด๊ฐ€ ๋งŒ๋“  ๋ฆฌ์•กํŠธ ์š”์†Œ๋ฅผ ์‹ค์ œ DOM์— ๋ฐ˜์˜ํ•˜๊ณ ์‹ถ๋‹ค๋ฉด ์ปดํฌ๋„ŒํŠธ๋กœ ๊ฐ์‹ธ react-dom ์— ๋„˜๊ฒจ์ฃผ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

import React from 'react'
import ReactDom from 'react-dom'


class ์ปดํฌ๋„ŒํŠธ ์ด๋ฆ„ extends React.Component{
  // ... ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ class ๋‚ด์šฉ ... //
  
  render(){ 
    return React.createElement("h1", {myProps: "์†์„ฑ๊ฐ’"}, " ๋‚ด์šฉ(Children) " );
  }
}


// myComponent๋ฅผ 'root' ๋ผ๋Š” ID๋ฅผ ๊ฐ€์ง„ ํƒœ๊ทธ ์•ˆ์— ๋ฐ˜์˜ํ•˜๋ผ๋Š” ์˜๋ฏธ.
ReactDOM.render(์ปดํฌ๋„ŒํŠธ, document.getElementById('root'));
import React from 'react'
import ReactDom from 'react-dom'


class myComponent extends React.Component{
  
  renderItem(item,i){ // ์š”์†Œ๋ฅผ ์ƒ์„ฑํ•˜๋Š” myComponent ๋ฉ”์„œ๋“œ
    return React.createElement("li", {key:i}, item)
  }
  
  render(){ // Component ๋‚ด์žฅ ๋ฉ”์„œ๋“œ. HTML์— ๋ Œ๋”๋งํ•  ๋ถ€๋ถ„
    return React.createElement("ul",
                               {className: "myClass"},
                               this.props.items.map(this.renderItem))
    // ์ด๋Ÿฐ์‹์œผ๋กœ ์ปดํฌ๋„ŒํŠธ ํด๋ž˜์Šค ๋‚ด์˜ ๋ฉ”์„œ๋“œ๋ฅผ ํ™œ์šฉ ํ•˜์—ฌ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  }
}


// ๋ฆฌ์•กํŠธ ๊ฐ€์ƒDOM์— ๋ Œ๋”๋งํ•ฉ๋‹ˆ๋‹ค.
// ๊ฐ€์ƒ DOM์—์„œ ์ด์ „ ๋ฌธ์„œ๋ฅผ ๋น„๊ตํ•ด ์•„๋‹Œ ๋ณ€๊ฒฝ๋œ ๋ถ€๋ถ„๋งŒ ํšจ์œจ์ ์œผ๋กœ ์ˆ˜์ •ํ•ฉ๋‹ˆ๋‹ค.
ReactDOM.render(myComponent, document.getElementById('root'));

์ด๋ ‡๊ฒŒ ๋ฐ”๋‹๋ผ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์™€ ๋ฆฌ์•กํŠธ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋กœ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑ ํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค.

๋ฆฌ์•กํŠธ์—์„œ ์‰ฝ๊ณ  ์ง๊ด€์ ์ด๊ฒŒ ์“ธ ์ˆ˜ ์žˆ๋Š” JSX๋ฌธ๋ฒ•๋„, ๊ฒฐ๊ตญ์—๋Š” Babel์„ ์ด์šฉํ•ด ์œ„์—์„œ ๋ณธ ์ฝ”๋“œ๋“ค๋„ ๋ณ€ํ™˜๋ฉ๋‹ˆ๋‹ค.

 

๋‹ค์Œ ๊ธ€์—์„œ๋Š” ๋ณธ๊ฒฉ์ ์œผ๋กœ JSX๋ฌธ๋ฒ•๊ณผ ๋ฆฌ์•กํŠธ์— ๋Œ€ํ•ด ์•Œ์•„๋ด…์‹œ๋‹ค.

๋ธ”๋กœ๊ทธ์˜ ์ •๋ณด

JiwonDev

JiwonDev

ํ™œ๋™ํ•˜๊ธฐ