createElement๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด React ์—˜๋ฆฌ๋จผํŠธ๋ฅผ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. JSX๋ฅผ ์ž‘์„ฑํ•˜๋Š” ๋Œ€์‹  ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

const element = createElement(type, props, ...children)

๋ ˆํผ๋Ÿฐ์Šค

createElement(type, props, ...children)

type, prop, children๋ฅผ ์ธ์ˆ˜๋กœ ์ œ๊ณตํ•˜๊ณ  createElement์„ ํ˜ธ์ถœํ•˜์—ฌ React ์—˜๋ฆฌ๋จผํŠธ๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.

import { createElement } from 'react';

function Greeting({ name }) {
return createElement(
'h1',
{ className: 'greeting' },
'Hello'
);
}

์•„๋ž˜์—์„œ ๋” ๋งŽ์€ ์˜ˆ์‹œ๋ฅผ ํ™•์ธํ•˜์„ธ์š”.

๋งค๊ฐœ๋ณ€์ˆ˜

  • type: type ์ธ์ˆ˜๋Š” ์œ ํšจํ•œ React ์ปดํฌ๋„ŒํŠธ์—ฌ์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ํƒœ๊ทธ ์ด๋ฆ„ ๋ฌธ์ž์—ด (์˜ˆ: โ€˜divโ€™, โ€˜spanโ€™) ๋˜๋Š” React ์ปดํฌ๋„ŒํŠธ(ํ•จ์ˆ˜, ํด๋ž˜์Šค, Fragment ๊ฐ™์€ ํŠน์ˆ˜ ์ปดํฌ๋„ŒํŠธ)๊ฐ€ ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

  • props: props ์ธ์ˆ˜๋Š” ๊ฐ์ฒด ๋˜๋Š” null์ด์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. null์„ ์ „๋‹ฌํ•˜๋ฉด ๋นˆ ๊ฐ์ฒด์™€ ๋™์ผํ•˜๊ฒŒ ์ฒ˜๋ฆฌ๋ฉ๋‹ˆ๋‹ค. React๋Š” ์ „๋‹ฌํ•œ props์™€ ์ผ์น˜ํ•˜๋Š” ํ”„๋กœํผํ‹ฐ๋ฅผ ๊ฐ€์ง„ ์—˜๋ฆฌ๋จผํŠธ๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. ์ „๋‹ฌํ•œ props ๊ฐ์ฒด์˜ ref์™€ key๋Š” ํŠน์ˆ˜ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ƒ์„ฑํ•œ ์—˜๋ฆฌ๋จผํŠธ์—์„œ element.props.ref ์™€ element.props.key๋Š” ์‚ฌ์šฉํ•  ์ˆ˜ ์—†๋‹ค๋Š” ์ ์— ์œ ์˜ํ•˜์„ธ์š”. element.ref ๋˜๋Š” element.key๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

  • ์„ ํƒ์‚ฌํ•ญ ...children: 0๊ฐœ ์ด์ƒ์˜ ์ž์‹ ๋…ธ๋“œ. React ์—˜๋ฆฌ๋จผํŠธ, ๋ฌธ์ž์—ด, ์ˆซ์ž, ํฌํƒˆ, ๋นˆ ๋…ธ๋“œ(null, undefined, true, false) ๊ทธ๋ฆฌ๊ณ  React ๋…ธ๋“œ ๋ฐฐ์—ด์„ ํฌํ•จํ•œ ๋ชจ๋“  React ๋…ธ๋“œ๊ฐ€ ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๋ฐ˜ํ™˜๊ฐ’

createElement๋Š” ์•„๋ž˜ ํ”„๋กœํผํ‹ฐ๋ฅผ ๊ฐ€์ง€๋Š” React ์—˜๋ฆฌ๋จผํŠธ ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

  • type: ์ „๋‹ฌ๋ฐ›์€ type.
  • props: ref์™€ key๋ฅผ ์ œ์™ธํ•œ ์ „๋‹ฌ๋ฐ›์€ props. type์ด ๋ ˆ๊ฑฐ์‹œ type.defaultProps๋ฅผ ๊ฐ€์ง€๋Š” ์ปดํฌ๋„ŒํŠธ๋ผ๋ฉด, ๋ˆ„๋ฝ๋˜๊ฑฐ๋‚˜ ์ •์˜๋˜์ง€ ์•Š์€ props๋Š” type.defaultProps ๊ฐ’์„ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค.
  • ref: ์ „๋‹ฌ๋ฐ›์€ ref. ๋ˆ„๋ฝ๋œ ๊ฒฝ์šฐ null.
  • key: ์ „๋‹ฌ๋ฐ›์€ key๋ฅผ ๊ฐ•์ œ ๋ณ€ํ™˜ํ•œ ๋ฌธ์ž์—ด. ๋ˆ„๋ฝ๋œ ๊ฒฝ์šฐ null.

์ผ๋ฐ˜์ ์œผ๋กœ ์—˜๋ฆฌ๋จผํŠธ๋Š” ์ปดํฌ๋„ŒํŠธ์—์„œ ๋ฐ˜ํ™˜๋˜๊ฑฐ๋‚˜ ๋‹ค๋ฅธ ์—˜๋ฆฌ๋จผํŠธ์˜ ์ž์‹์œผ๋กœ ๋งŒ๋“ญ๋‹ˆ๋‹ค. ์—˜๋ฆฌ๋จผํŠธ์˜ ํ”„๋Ÿฌํผํ‹ฐ์—๋Š” ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ์ง€๋งŒ, ์—˜๋ฆฌ๋จผํŠธ ์ƒ์„ฑ ํ›„์—๋Š” ๋ชจ๋“  ์—˜๋ฆฌ๋จผํŠธ์— ์ ‘๊ทผํ•  ์ˆ˜ ์—†๋Š” ๊ฒƒ์ฒ˜๋Ÿผ ๋Œ€ํ•˜๊ณ  ๋ Œ๋”๋ง๋งŒ ํ•˜๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค.


์ฃผ์˜ ์‚ฌํ•ญ


  • ๋ฐ˜๋“œ์‹œ ๋ฆฌ์•กํŠธ ์—˜๋ฆฌ๋จผํŠธ์™€ ๊ทธ ํ”„๋Ÿฌํผํ‹ฐ๋Š” ๋ถˆ๋ณ€ํ•˜๊ฒŒ ์ทจ๊ธ‰ํ•ด์•ผํ•˜๋ฉฐ ์—˜๋ฆฌ๋จผํŠธ ์ƒ์„ฑ ํ›„์—๋Š” ๊ทธ ๋‚ด์šฉ์ด ๋ณ€๊ฒฝ๋˜์–ด์„  ์•ˆ ๋ฉ๋‹ˆ๋‹ค. ๊ฐœ๋ฐœํ™˜๊ฒฝ์—์„œ ๋ฆฌ์•กํŠธ๋Š” ์ด๋ฅผ ๊ฐ•์ œํ•˜๊ธฐ ์œ„ํ•ด ๋ฐ˜ํ™˜๋œ ์—˜๋ฆฌ๋จผํŠธ์™€ ๊ทธ ํ”„๋กœํผํ‹ฐ๋ฅผ ์–•๊ฒŒ freezeํ•ฉ๋‹ˆ๋‹ค.

  • JSX๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค๋ฉด ํƒœ๊ทธ๋ฅผ ๋Œ€๋ฌธ์ž๋กœ ์‹œ์ž‘ํ•ด์•ผ๋งŒ ์‚ฌ์šฉ์ž ์ปดํฌ๋„ŒํŠธ๋ฅผ ๋ Œ๋”๋งํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ฆ‰, <Something />์€ createElement(Something)๊ณผ ๋™์ผํ•˜์ง€๋งŒ <something />(์†Œ๋ฌธ์ž) ์€ createElement('something')์™€ ๋™์ผํ•ฉ๋‹ˆ๋‹ค. (๋ฌธ์ž์—ด์ž„์„ ์ฃผ์˜ํ•˜์„ธ์š”. ๋‚ด์žฅ๋œ HTML ํƒœ๊ทธ๋กœ ์ทจ๊ธ‰๋ฉ๋‹ˆ๋‹ค.)

  • createElement('h1', {}, child1, child2, child3)์™€ ๊ฐ™์ด children์ด ๋ชจ๋‘ ์ •์ ์ธ ๊ฒฝ์šฐ์—๋งŒ createElement์— ์—ฌ๋Ÿฌ ์ธ์ˆ˜๋กœ ์ „๋‹ฌํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. children์ด ๋™์ ์ด๋ผ๋ฉด ์ „์ฒด ๋ฐฐ์—ด์„ ์„ธ ๋ฒˆ์งธ ์ธ์ˆ˜๋กœ ์ „๋‹ฌํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด React๋Š” ๋ˆ„๋ฝ๋œ ํ‚ค์— ๋Œ€ํ•œ ๊ฒฝ๊ณ ๋ฅผ ํ‘œ์‹œํ•ฉ๋‹ˆ๋‹ค. ์ •์  ๋ชฉ๋ก์ธ ๊ฒฝ์šฐ ์žฌ์ •๋ ฌํ•˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— ์ž‘์—…์ด ํ•„์š”ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.


์‚ฌ์šฉ๋ฒ•

JSX ์—†์ด ์—˜๋ฆฌ๋จผํŠธ ์ƒ์„ฑํ•˜๊ธฐ

JSX๊ฐ€ ๋งˆ์Œ์— ๋“ค์ง€ ์•Š๊ฑฐ๋‚˜ ํ”„๋กœ์ ํŠธ์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†๋Š” ๊ฒฝ์šฐ, createElement๋ฅผ ๋Œ€์•ˆ์œผ๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

JSX ์—†์ด ์—˜๋ฆฌ๋จผํŠธ๋ฅผ ์ƒ์„ฑํ•˜๋ ค๋ฉด type, props, children์™€ ํ•จ๊ป˜ createElement๋ฅผ ํ˜ธ์ถœํ•ฉ๋‹ˆ๋‹ค

import { createElement } from 'react';

function Greeting({ name }) {
return createElement(
'h1',
{ className: 'greeting' },
'Hello ',
createElement('i', null, name),
'. Welcome!'
);
}

children์€ ์„ ํƒ ์‚ฌํ•ญ์ด๋ฉฐ ํ•„์š”ํ•œ ๋งŒํผ ์ „๋‹ฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. (์œ„ ์˜ˆ์‹œ์—๋Š” 3๊ฐœ์˜ children์ด ์žˆ์Šต๋‹ˆ๋‹ค.) ์œ„ ์ฝ”๋“œ๋Š” ์ธ์‚ฌ๋ง์ด ํฌํ•จ๋œ <h1>๋ฅผ ํ‘œ์‹œํ•ฉ๋‹ˆ๋‹ค. ๋น„๊ต๋ฅผ ์œ„ํ•ด ๋™์ผํ•œ ์˜ˆ์‹œ๋ฅผ JSX๋กœ ์žฌ์ž‘์„ฑํ–ˆ์Šต๋‹ˆ๋‹ค.

function Greeting({ name }) {
return (
<h1 className="greeting">
Hello <i>{name}</i>. Welcome!
</h1>
);
}

์ž์‹ ๋งŒ์˜ React ์ปดํฌ๋„ŒํŠธ๋ฅผ ๋ Œ๋”๋งํ•˜๋ ค๋ฉด 'h1' ๊ฐ™์€ ๋ฌธ์ž์—ด ๋Œ€์‹  Greeting ๊ฐ™์€ ํ•จ์ˆ˜๋ฅผ type์— ์ „๋‹ฌํ•˜์„ธ์š”.

export default function App() {
return createElement(Greeting, { name: 'Taylor' });
}

JSX๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

export default function App() {
return <Greeting name="Taylor" />;
}

createElement๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ž‘์„ฑํ•œ ์ „์ฒด ์˜ˆ์‹œ์ž…๋‹ˆ๋‹ค.

import { createElement } from 'react';

function Greeting({ name }) {
  return createElement(
    'h1',
    { className: 'greeting' },
    'Hello ',
    createElement('i', null, name),
    '. Welcome!'
  );
}

export default function App() {
  return createElement(
    Greeting,
    { name: 'Taylor' }
  );
}

JSX๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ž‘์„ฑํ•œ ์ „์ฒด ์˜ˆ์‹œ์ž…๋‹ˆ๋‹ค.

function Greeting({ name }) {
  return (
    <h1 className="greeting">
      Hello <i>{name}</i>. Welcome!
    </h1>
  );
}

export default function App() {
  return <Greeting name="Taylor" />;
}

๋‘ ์ฝ”๋”ฉ ์Šคํƒ€์ผ ๋ชจ๋‘ ํ—ˆ์šฉ๋˜๋ฏ€๋กœ ํ”„๋กœ์ ํŠธ์— ๋งž๋Š” ์Šคํƒ€์ผ์„ ์‚ฌ์šฉํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. createElement์™€ ๋น„๊ตํ•˜์—ฌ JSX๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ์˜ ์žฅ์ ์€ ์–ด๋–ค ๋‹ซ๋Š” ํƒœ๊ทธ๊ฐ€ ์–ด๋–ค ์—ฌ๋Š” ํƒœ๊ทธ์— ๋Œ€์‘๋˜๋Š”์ง€ ์‰ฝ๊ฒŒ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค.

Deep Dive

React ์—˜๋ฆฌ๋จผํŠธ๋ž€ ์ •ํ™•ํžˆ ๋ฌด์—‡์ธ๊ฐ€์š”?

์—˜๋ฆฌ๋จผํŠธ๋Š” ์‚ฌ์šฉ์ž ์ธํ„ฐํŽ˜์ด์Šค์˜ ์ผ๋ถ€์— ๋Œ€ํ•œ ํ‘œํ˜„์ž…๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด <Greeting name="Taylor" />์™€ createElement(Greeting, { name: 'Taylor' })๋Š” ๋ชจ๋‘ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.

// ์•ฝ๊ฐ„ ๋‹จ์ˆœํ™”๋จ
{
type: Greeting,
props: {
name: 'Taylor'
},
key: null,
ref: null,
}

์ด ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•ด๋„ Greeting ์ปดํฌ๋„ŒํŠธ๊ฐ€ ๋ Œ๋”๋ง ๋˜๊ฑฐ๋‚˜ DOM ์—˜๋ฆฌ๋จผํŠธ๊ฐ€ ์ƒ์„ฑ๋˜์ง€๋Š” ์•Š๋Š”๋‹ค๋Š” ์ ์„ ์ฃผ์˜ํ•˜์„ธ์š”.

๋ฆฌ์•กํŠธ ์—˜๋ฆฌ๋จผํŠธ๋Š” ๋‚˜์ค‘์— React๊ฐ€ Greeting ์ปดํฌ๋„ŒํŠธ๋ฅผ ๋ Œ๋”๋งํ•˜๋„๋ก ์ง€์‹œํ•˜๋Š” ์„ค๋ช…์„œ์™€ ๋น„์Šทํ•ฉ๋‹ˆ๋‹ค. App ์ปดํฌ๋„ŒํŠธ์—์„œ ์ด ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜ํ•จ์œผ๋กœ์จ React์—๊ฒŒ ๋‹ค์Œ ํ•  ์ผ์„ ์ง€์‹œํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์—˜๋ฆฌ๋จผํŠธ ์ƒ์„ฑ ๋น„์šฉ์€ ๋งค์šฐ ์ €๋ ดํ•˜๋ฏ€๋กœ ์—˜๋ฆฌ๋จผํŠธ ์ƒ์„ฑ์„ ์ตœ์ ํ™”ํ•˜๊ฑฐ๋‚˜ ํ”ผํ•˜๋ ค๊ณ  ๋…ธ๋ ฅํ•  ํ•„์š”๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค.