Deprecated

์ด API๋Š” ํ–ฅํ›„ React์˜ ์ฃผ์š” ๋ฒ„์ „์—์„œ ์ œ๊ฑฐ๋  ์˜ˆ์ •์ž…๋‹ˆ๋‹ค. ๋Œ€์•ˆ๋“ค์„ ์‚ดํŽด๋ณด์„ธ์š”.

createFactory๋Š” ํŠน์ • type์˜ React ์—˜๋ฆฌ๋จผํŠธ๋ฅผ ๋งŒ๋“œ๋Š” ํ•จ์ˆ˜๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.

const factory = createFactory(type)

๋ ˆํผ๋Ÿฐ์Šค

createFactory(type)

์ฃผ์–ด์ง„ type์˜ React ์—˜๋ฆฌ๋จผํŠธ๋ฅผ ๋งŒ๋“ค์–ด ๋‚ด๋Š” ํŒฉํ† ๋ฆฌ ํ•จ์ˆ˜๋ฅผ ์ƒ์„ฑํ•˜๊ธฐ ์œ„ํ•ด createFactory(type)๋ฅผ ํ˜ธ์ถœํ•˜์„ธ์š”.

import { createFactory } from 'react';

const button = createFactory('button');

์ดํ›„ JSX ์—†์ด React ์—˜๋ฆฌ๋จผํŠธ๋ฅผ ๋งŒ๋“ค๊ธฐ ์œ„ํ•ด, ํ•ด๋‹น ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

export default function App() {
return button({
onClick: () => {
alert('Clicked!')
}
}, 'Click me');
}

์•„๋ž˜์—์„œ ๋” ๋งŽ์€ ์‚ฌ์šฉ๋ฒ•์„ ํ™•์ธํ•˜์„ธ์š”.

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

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

๋ฐ˜ํ™˜ ๊ฐ’

ํŒฉํ† ๋ฆฌ ํ•จ์ˆ˜๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ์ด ํ•จ์ˆ˜๋Š” ์ž์‹ ์ธ์ˆ˜์˜ ๋ฆฌ์ŠคํŠธ์— ๋’ค์ด์–ด, ์ฒซ ๋ฒˆ์งธ ์ธ์ˆ˜๋กœ props ๊ฐ์ฒด๋ฅผ ๋ฐ›์œผ๋ฉฐ, ์ฃผ์–ด์ง„ types, props ๊ทธ๋ฆฌ๊ณ  ์ž์‹์„ ๊ฐ€์ง„ React ์—˜๋ฆฌ๋จผํŠธ๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.


์‚ฌ์šฉ๋ฒ•

ํŒฉํ† ๋ฆฌ ํ•จ์ˆ˜๋กœ React ์—˜๋ฆฌ๋จผํŠธ ๋งŒ๋“ค๊ธฐ

๋น„๋ก ๋Œ€๋ถ€๋ถ„์˜ React ํ”„๋กœ์ ํŠธ๋“ค์€ JSX๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์œ ์ € ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ํ‘œํ˜„ํ•˜์ง€๋งŒ, JSX๊ฐ€ ํ•„์ˆ˜๋Š” ์•„๋‹™๋‹ˆ๋‹ค. ๊ณผ๊ฑฐ์—๋Š” createFactory๋ฅผ JSX ์—†์ด ์œ ์ € ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ํ‘œํ˜„ํ•˜๋Š” ๋ฐฉ๋ฒ•์˜ ํ•˜๋‚˜๋กœ ์‚ฌ์šฉํ•˜์˜€์Šต๋‹ˆ๋‹ค.

button๊ณผ ๊ฐ™์ด ํŠน์ • ์—˜๋ฆฌ๋จผํŠธ type์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ํŒฉํ† ๋ฆฌ ํ•จ์ˆ˜๋ฅผ ์ƒ์„ฑํ•˜๊ธฐ ์œ„ํ•ด createFactory๋ฅผ ํ˜ธ์ถœํ•ฉ๋‹ˆ๋‹ค.

import { createFactory } from 'react';

const button = createFactory('button');

๊ทธ ๋‹ค์Œ, ์ œ๊ณต๋œ props์™€ ์ž์‹์œผ๋กœ React ์—˜๋ฆฌ๋จผํŠธ๋ฅผ ๋งŒ๋“ค์–ด๋‚ด๋Š” ํŒฉํ† ๋ฆฌ ํ•จ์ˆ˜๋ฅผ ์‹คํ–‰ํ•ฉ๋‹ˆ๋‹ค.

import { createFactory } from 'react';

const button = createFactory('button');

export default function App() {
  return button({
    onClick: () => {
      alert('Clicked!')
    }
  }, 'Click me');
}

์ด๋Š” createFactory์„ JSX์˜ ๋Œ€์•ˆ์œผ๋กœ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ createFactory๋Š” ๋” ์ด์ƒ ์‚ฌ์šฉํ•˜์ง€ ์•Š์œผ๋ฉฐ, ์ดํ›„ ์ƒˆ๋กœ์šด ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•  ๋•Œ createFactory๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š์•„์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์•„๋ž˜์—์„œ createFactory ๋Œ€์‹  ๋‹ค๋ฅธ ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ์‹์„ ์‚ดํŽด๋ณด์„ธ์š”.


๋Œ€์•ˆ

ํ”„๋กœ์ ํŠธ์— createFactory ๋ณต์‚ฌํ•˜๊ธฐ

๋งŒ์•ฝ ํ”„๋กœ์ ํŠธ์— createFactory๊ฐ€ ๋งŽ์ด ์‚ฌ์šฉ๋œ๋‹ค๋ฉด, ๋‹ค์Œ์˜ createFactory.js ๋‚ด์šฉ์„ ํ”„๋กœ์ ํŠธ ๋‚ด๋ถ€์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก ๋ณต์‚ฌํ•˜์„ธ์š”.

import { createFactory } from './createFactory.js';

const button = createFactory('button');

export default function App() {
  return button({
    onClick: () => {
      alert('Clicked!')
    }
  }, 'Click me');
}

์ด๋Ÿฌํ•œ ์ž‘์—…์„ ํ†ตํ•ด, import ๋ฌธ์„ ์ œ์™ธํ•˜๊ณ  ๋‹ค๋ฅธ ์ฝ”๋“œ๋ฅผ ๋ฐ”๊พธ์ง€ ์•Š์€ ์ƒํƒœ๋กœ ์œ ์ง€ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.


createFactory๋ฅผ createElement๋กœ ๋Œ€์ฒดํ•˜๊ธฐ

์ง์ ‘ ์˜ฎ๊ฒจ์™€๋„ ๋ฌด๋ฐฉํ•  ์ •๋„๋กœ createFactory๋ฅผ ๋ช‡ ๊ฐœ๋งŒ ํ˜ธ์ถœํ•˜๊ณ  ์žˆ๊ณ  JSX๋ฅผ ์‚ฌ์šฉํ•˜๊ณ  ์‹ถ์ง€ ์•Š๋‹ค๋ฉด, createElement๋ฅผ ์‹คํ–‰ํ•˜์—ฌ ํŒฉํ† ๋ฆฌ ํ•จ์ˆ˜๋ฅผ ๋Œ€์ฒดํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ์ด ์ฝ”๋“œ๋Š”,

import { createFactory } from 'react';

const button = createFactory('button');

export default function App() {
return button({
onClick: () => {
alert('Clicked!')
}
}, 'Click me');
}

์•„๋ž˜์™€ ๊ฐ™์ด ๋ฐ”๊ฟ€ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

import { createElement } from 'react';

export default function App() {
return createElement('button', {
onClick: () => {
alert('Clicked!')
}
}, 'Click me');
}

์ตœ์ข…์ ์œผ๋กœ JSX ์—†์ด React๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ์˜ˆ์‹œ์ž…๋‹ˆ๋‹ค.

import { createElement } from 'react';

export default function App() {
  return createElement('button', {
    onClick: () => {
      alert('Clicked!')
    }
  }, 'Click me');
}


createFactory๋ฅผ JSX๋กœ ๋Œ€์ฒดํ•˜๊ธฐ

๋งˆ์ง€๋ง‰์œผ๋กœ createFactory ๋Œ€์‹  JSX๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Š” React๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด ๊ฐ€์žฅ ํ”ํ•˜๊ฒŒ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค.

export default function App() {
  return (
    <button onClick={() => {
      alert('Clicked!');
    }}>
      Click me
    </button>
  );
};

์ฃผ์˜ํ•˜์„ธ์š”!

button๊ณผ ๊ฐ™์€ ์ƒ์ˆ˜ ๋Œ€์‹  type์„ ํŠน์ • ๋ณ€์ˆ˜๋กœ ์‚ฌ์šฉํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค.

function Heading({ isSubheading, ...props }) {
const type = isSubheading ? 'h2' : 'h1';
const factory = createFactory(type);
return factory(props);
}

JSX๋ฅผ ์‚ฌ์šฉํ•ด ๊ฐ™์€ ๋ฐฉ์‹์œผ๋กœ ๊ตฌํ˜„ํ•œ๋‹ค๋ฉด Type์ฒ˜๋Ÿผ ๋Œ€๋ฌธ์ž๋กœ ์‹œ์ž‘ํ•˜๋Š” ๋ณ€์ˆ˜ ์ด๋ฆ„์„ ์ƒˆ๋กญ๊ฒŒ ์„ค์ •ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

function Heading({ isSubheading, ...props }) {
const Type = isSubheading ? 'h2' : 'h1';
return <Type {...props} />;
}

๊ทธ๋ ‡๊ฒŒ ํ•˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ React๋Š” ์†Œ๋ฌธ์ž๋กœ ์ž‘์„ฑ๋œ <type>์„ ๋‚ด์žฅ๋œ HTML ํƒœ๊ทธ๋กœ ํ•ด์„ํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค.