๐Ÿง  ์ œ์–ด์˜ ์—ญ์ „(Inversion of Control)

Lee Jooamยท2022๋…„ 6์›” 10์ผ
3
post-thumbnail

๐Ÿ’Œ ์š”์•ฝ

The principle of IoC says that we don't call the framework, the framework calls us.

์™ธ๋ถ€์˜ API(๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์ œ๊ณต API, ์–ธ์–ด ์ œ๊ณต ๋ฉ”์†Œ๋“œ ๋“ฑ)์—์„œ ์‹คํ–‰๋  ์ž‘์—…์„ ์‚ฌ์šฉ์ž(ํ”„๋กœ๊ทธ๋ž˜๋จธ) ์ชฝ์œผ๋กœ ๋„˜๊ธฐ๋Š” ๊ฒƒ


๐Ÿ“ฃ ๋ฌธ์ œ ์ธ์‹

์ข‹์€ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ๋ฐฉ์‹ ์ค‘ ํ•˜๋‚˜๋Š” ๋ณดํŽธ์ ์ธ ๊ธฐ๋Šฅ์— ์ถ”์ƒํ™” ๊ณ„์ธต์„ ์ œ๊ณตํ•˜๊ณ  ๊ทธ๊ฒƒ์„ ์žฌ์‚ฌ์šฉํ•˜๋„๋ก ํ•˜๋Š” ๊ฒƒ์ด๋‹ค.

๋‹ค์Œ๊ณผ ๊ฐ™์€ ์˜ˆ์‹œ๋ฅผ ๋“ค ์ˆ˜ ์žˆ๊ฒ ๋‹ค.

function calculator(a, b, option) {
  switch (option) {
    case "add":
      return a + b;
    case "sub":
      return a - b;
    case "mul":
      return a * b;
    case "div":
      return a / b;
    default:
      return 0;
  }
}

console.log(calculator(1, 5, "sub"));

๋งŒ์•ฝ ๊ณ„์‚ฐ ์ž‘์—…์„ ํ•˜๋Š” ๊ธฐ๋Šฅ์„ ์žฌ์‚ฌ์šฉ์„ฑ์„ ์œ„ํ•ด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋งŒ๋“ค์—ˆ๋‹ค๊ณ  ํ•ด๋ณด์ž. ์žฌ์‚ฌ์šฉ ์ธก๋ฉด์—์„œ ์ถฉ๋ถ„ํžˆ ์ œ ๊ธฐ๋Šฅ์„ ํ•˜๋Š” ํ•จ์ˆ˜์ด์ง€๋งŒ ๋ช‡ ๊ฐ€์ง€ ๋‹จ์ ์ด ์žˆ๋‹ค.

์šฐ๋ฆฌ๊ฐ€ ๋ช‡ ๊ฐœ์˜ ๊ธฐ๋Šฅ์„ ๋” ์ถ”๊ฐ€ํ•œ๋‹ค๊ณ  ๊ฐ€์ •ํ•ด๋ณด์ž. ๊ฐ๊ฐ a์™€, b๋ฅผ ์ œ๊ณฑํ•ด์„œ ๋”ํ•˜๋Š” ๊ฒƒ, ๊ทธ๋ฆฌ๊ณ  a์™€ b์˜ ์ ˆ๋Œ“๊ฐ’์„ ๊ตฌํ•˜๋Š” ๊ธฐ๋Šฅ์„ ์ถ”๊ฐ€ํ•ด๋ณด์ž.

function calculator(a, b, option) {
  switch (option) {
    case "add":
      return a + b;
    case "sub":
      return a - b;
    case "mul":
      return a * b;
    case "div":
      return a / b;
    case "power":
      return a ** 2 + b ** 2;
    case "abs":
      return Math.abs(a - b);
    default:
      return 0;
  }
}

console.log(calculator(3, 6, "power"));
console.log(calculator(1, 5, "abs"));

๋‹ค์Œ๊ณผ ๊ฐ™์€ ํ˜•ํƒœ๋กœ ๊ธฐ๋Šฅ์ด ์ˆ˜์ •๋  ๊ฒƒ ๊ฐ™๋‹ค.

๋ฒŒ์จ๋ถ€ํ„ฐ ๋‚˜์œ ๋ƒ„์ƒˆ๊ฐ€ ํ’€ํ’€ ํ’๊ธด๋‹ค. ๊ธฐ๋Šฅ์„ ์ถ”๊ฐ€ํ•˜๋ฉด ํ• ์ˆ˜๋ก ํ•จ์ˆ˜๋Š” ๊ฑฐ๋Œ€ํ•ด์ง€๊ณ , ๊ทธ๊ฒƒ์„ ์‚ฌ์šฉํ•˜๋Š” ์‚ฌ์šฉ์ž ๋˜ํ•œ ํ•จ์ˆ˜์˜ ์‚ฌ์šฉ๋ฒ•์„ ์ตํžˆ๋Š” ๋ฐ ๋ถ€๋‹ด์„ ๋Š๋‚„ ๊ฒƒ์ด๋‹ค.

๋‹น์—ฐํžˆ ์œ ์ง€๋ณด์ˆ˜ ์ธก๋ฉด์—์„œ๋„ ์•ˆ ์ข‹์•„์ง€๊ธฐ ๋•Œ๋ฌธ์—, ์š”๊ตฌ ์‚ฌํ•ญ์„ ๋ชจ๋‘ ๋ฐ˜์˜ํ•˜๊ธฐ์—๋Š” ์ด๋Ÿฐ ๊ตฌ์กฐ๊ฐ€ ์ข‹์€ ๋ฐฉ์‹์€ ์•„๋‹ˆ๋‹ค.


๐Ÿ”‘ ๊ฐœ์„ 

function IOCCalculator(a, b, callback) {
  return callback(a, b);
}

console.log(IOCCalculator(5, 6, (a, b) => a + b));
console.log(IOCCalculator(5, 6, (a, b) => a ** 2 + b ** 2));

์œ„์™€ ๊ฐ™์€ ๊ตฌ์กฐ๋กœ ๊ฐœ์„ ํ–ˆ๋‹ค.

์‚ฌ์‹ค ๊ฐœ์„ ์ด๋ผ๊ณ  ๋งํ•˜๊ธฐ๋Š” ์กฐ์‹ฌ์Šค๋Ÿฌ์šด ๋ถ€๋ถ„์ด์ง€๋งŒ, ๋งŒ์•ฝ ์ถ”๊ฐ€์ ์ธ ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•œ๋‹ค๋ฉด ํ•ด๋‹น ํ•จ์ˆ˜ ๋‚ด์— ๋กœ์ง์„ ์ถ”๊ฐ€ํ•ด ๋กœ๊น…์ด๋‚˜, validation ์ฒดํฌ ๋“ฑ ๋‹ค์–‘ํ•œ ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•  ์ˆ˜ ์žˆ๋‹ค.

function IOCCalculator(a, b, callback) {
  console.log(`${a} calc ${b} = ${callback(a, b)});

  return a < b ? callback(a, b) : 0;
}

console.log(IOCCalculator(5, 6, (a, b) => a + b));
console.log(IOCCalculator(5, 6, (a, b) => a ** 2 + b ** 2));

์ด๋Ÿฐ ๋ฐฉ์‹์ด ๋ฐ”๋กœ ์ œ์–ด์˜ ์—ญ์ „์ด๋‹ค. ์ œ์–ด์˜ ์—ญ์ „์ด๋ผ๋Š” ๋ง์ด ์กฐ๊ธˆ ์–ด๋ ต๋‹ค. ์™œ ๊ทธ๋Ÿฐ ์ด๋ฆ„์œผ๋กœ ๋ถ€๋ฅด๋Š” ์ง€ ์‚ดํŽด๋ณด์ž.


๐Ÿ˜ฅ ์ผ๋ฐ˜์ ์ธ ํ”„๋กœ๊ทธ๋ž˜๋ฐ

๊ธฐ๋ณธ์ ์ธ ํ”„๋กœ๊ทธ๋ž˜๋ฐ์—์„œ ํ”„๋กœ๊ทธ๋žจ์˜ ํ๋ฆ„์€ ๋ช…๋ นํ˜•์œผ๋กœ ์ง„ํ–‰๋œ๋‹ค.

์˜ˆ๋ฅผ ๋“ค์–ด ์ข…์ด ๋น„ํ–‰๊ธฐ๋ฅผ ์ ‘๋Š”๋‹ค๊ณ  ํ–ˆ์„ ๋•Œ๋ฅผ ์ƒ๊ฐํ•ด๋ณด์ž.

  1. ์ข…์ด๋ฅผ ๋ฐ˜์œผ๋กœ ์ ‘๊ณ  ํŽธ๋‹ค.
  2. ์œ„์ชฝ ๋ชจ์„œ๋ฆฌ๋ฅผ ์ค‘์•™ ์„ ์— ๋งž์ถฐ ์‚ผ๊ฐํ˜• ๋ชจ์–‘์œผ๋กœ ์ ‘๋Š”๋‹ค.
  3. ์œ„์ชฝ ๋ชจ์„œ๋ฆฌ๋ฅผ ์ ‘ํ˜€์žˆ๋Š” ์ƒํƒœ์—์„œ ํ•œ๋ฒˆ ๋” ์ ‘๋Š”๋‹ค.
  4. ์ข…์ด๋ฅผ ๋ฐ˜์œผ๋กœ ์ ‘๋Š”๋‹ค.
  5. ์–‘ ์˜†์„...

์ด์ฒ˜๋Ÿผ ๋ชจ๋“  ๋™์ž‘๋“ค์„ ํ•˜๋‚˜ํ•˜๋‚˜, ๊ทธ๋ฆฌ๊ณ  ์ˆœ์„œ์— ๋งž์ถฐ ์ˆ˜ํ–‰ํ•ด์•ผํ•œ๋‹ค.

์ด๋ ‡๊ฒŒ ์„ธ๋ถ€์ ์ธ ์š”์†Œ๋“ค์„ ๋ชจ๋‘ ์–ธ์–ด๋ฅผ ์ž‘์„ฑํ•˜๋Š” ์ชฝ์—์„œ ์ˆ˜ํ–‰ํ•˜๋Š” ๊ฒŒ ์ผ๋ฐ˜์ ์ธ ํ”„๋กœ๊ทธ๋ž˜๋ฐ์˜ ํ๋ฆ„์ด๋‹ค.

์œ„์˜ ์˜ˆ์‹œ์—์„œ ์ดˆ๊ธฐ calculator๊ฐ€ ๊ทธ๋Ÿฐ ๋ฐฉ์‹์ด๋‹ค. ์ด๋Ÿฐ ๊ตฌํ˜„์˜ ๋‹จ์ ์€ ๋ชจ๋“  usecase์— ๋Œ€์‘ํ•  ์ˆ˜ ์žˆ๋„๋ก ๊ธฐ๋Šฅ์„ ์ž‘์„ฑํ•ด์•ผ ํ•œ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค.

๋˜ํ•œ ์ถ”ํ›„ ๊ธฐ๋Šฅ์„ ๊ฐœ์„ ํ•  ๋•Œ, ๋ถˆํ•„์š”ํ•˜๋‹ค๊ณ  ์ƒ๊ฐ๋˜๋Š” ๊ธฐ๋Šฅ์„ ๋นผ๊ณ  ์‹ถ์–ด๋„ ์‰ฝ์ง€ ์•Š๋‹ค. ๊ธฐ์กด์— ํ•ด๋‹น ๊ธฐ๋Šฅ์„ ์‚ฌ์šฉํ•˜๋Š” ๋ถ€๋ถ„์ด ์žˆ์—ˆ๋‹ค๋ฉด ๋ฌธ์ œ๋ฅผ ๋ฐœ์ƒ์‹œํ‚ค๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

ํ•˜์ง€๋งŒ ์ˆ˜ํ–‰๋˜์–ด์•ผ ํ•  ๊ธฐ๋Šฅ์˜ ์ผ๋ถ€๋ถ„์„ ์‚ฌ์šฉ์ž(ํ”„๋กœ๊ทธ๋ž˜๋จธ) ์ธก์œผ๋กœ ๋„˜๊ธด๋‹ค๋ฉด ๋‹ค์–‘ํ•œ usecase์— ํšจ์œจ์ ์œผ๋กœ ๋Œ€์‘ํ•  ์ˆ˜ ์žˆ๋‹ค.

์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ–ˆ์ง€๋งŒ ๊ทธ๊ฒƒ์„ ์‹คํ–‰ํ•˜๋Š” ๊ฒƒ์€ ํ˜ธ์ถœํ•œ ๋ชจ๋“ˆ์ด ๋˜๋Š” ๊ฒƒ์ด๋‹ค. ์ด๋ ‡๊ฒŒ ์ผ๋ฐ˜์ ์ธ ํ๋ฆ„์—์„œ ๋ฒ—์–ด๋‚œ ํ˜•ํƒœ์˜ ์„ค๊ณ„๋ฅผ ์ œ์–ด์˜ ์—ญ์ „(Inversion of Control)์ด๋ผ๊ณ  ํ•œ๋‹ค.

์ฆ‰ ์‚ฌ์šฉ์ž๊ฐ€ ์ž‘์„ฑํ•œ ์ฝ”๋“œ๊ฐ€ ํ•ด๋‹น ๊ธฐ๋Šฅ์˜ ์ผ๋ถ€๋ถ„์œผ๋กœ ์ž‘๋™ํ•˜๊ฒŒ ๋˜๋Š” ๊ฒƒ์ด๋‹ค.

๐Ÿ˜€ ํ”„๋ก ํŠธ์—”๋“œ์—์„œ์˜ ์ œ์–ด์˜ ์—ญ์ „

์ƒ๊ฐ๋ณด๋‹ค ์ œ์–ด์˜ ์—ญ์ „์€ ํ”ํ•˜๊ฒŒ ์ผ๋‹ค. ๋‹จ์ง€ ๊ทธ๊ฒƒ์ด ์ œ์–ด์˜ ์—ญ์ „์ด๋ผ๋Š” ๊ฐœ๋…์ธ์ง€๋Š” ๋ชจ๋ฅด๊ณ  ์žˆ์—ˆ์ง€๋งŒ... map, filter ๋“ฑ ๋‹ค์–‘ํ•œ ๋ฐฐ์—ด ๋ฉ”์†Œ๋“œ ๋˜ํ•œ ์ œ์–ด์˜ ์—ญ์ „ ํŒจํ„ด์ด๋ผ๊ณ  ๋ณผ ์ˆ˜ ์žˆ๋‹ค.

const Board = ({ author, title, content, date }) => {
  return (
    <div>
      <div>์ž‘์„ฑ์ž: {author}</div>
      <div>
        ์ œ๋ชฉ: {title}, ๋‚ ์งœ {date}
      </div>
      <div>๋‚ด์šฉ: {content}</div>
    </div>
  );
};

function App() {
  const { author, title, content, date } = {
    author: "lee",
    title: "My simple Board",
    content: "Hello this is my First Board",
    date: "today",
  };
  return (
    <div className='App'>
      <Board author={author} title={title} content={content} date={date} />
    </div>
  );
}

๋‹ค์Œ๊ณผ ๊ฐ™์€ ์ปดํฌ๋„ŒํŠธ๊ฐ€ ์žˆ๋‹ค๊ณ  ํ•ด๋ณด์ž. Board ์ปดํฌ๋„ŒํŠธ์˜ ๋ฌธ์ œ์ ์„ ์ฐพ์•„๋ณด์ž.

Board ์ปดํฌ๋„ŒํŠธ๋Š” props๋กœ ์†์„ฑ๋“ค์„ ๋ฐ›์•„ ๊ทธ๊ฒƒ์„ ๋ Œ๋”๋งํ•˜๊ณ  ์žˆ๋‹ค. ์ด๋•Œ ๋ฌธ์ œ๋Š” ์–ด๋–ค props๊ฐ€ ๋“ค์–ด์˜ค๋Š”์ง€ ์ •ํ™•ํžˆ ์•Œ์•„์•ผ ํ•˜๊ณ , ๋งŒ์•ฝ ์ƒˆ๋กœ์šด props๋ฅผ ์ถ”๊ฐ€ํ•ด์•ผ ํ•œ๋‹ค๋ฉด ์‚ฌ์šฉ ์ธก๊ณผ ์ปดํฌ๋„ŒํŠธ ์ธก ๋ชจ๋‘ ๊ทธ๊ฒƒ์„ ๋ฐ˜์˜ํ•ด์•ผํ•œ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค.

const Board = ({ author, title, content, date, onClick }) => {
  return (
    <div
      onClick={() => {
        onClick(author);
      }}
    >
      <div>์ž‘์„ฑ์ž: {author}</div>
      <div>
        ์ œ๋ชฉ: {title}, ๋‚ ์งœ {date}
      </div>
      <div>๋‚ด์šฉ: {content}</div>
    </div>
  );
};

function App() {
  const { author, title, content, date, onClick } = {
    author: "lee",
    title: "My simple Board",
    content: "Hello this is my First Board",
    date: "today",
    onClick: () => {
      alert(`Hello!`);
    },
  };
  return (
    <div className='App'>
      <Board
        author={author}
        title={title}
        content={content}
        date={date}
        onClick={onClick}
      />
    </div>
  );
}

๋งŒ์•ฝ ์ปดํฌ๋„ŒํŠธ์˜ ์š”๊ตฌ์‚ฌํ•ญ์ด ๋‹ฌ๋ผ์ ธ ํŠน์ • ์š”์†Œ๋“ค์„ ์ถ”๊ฐ€ํ•ด์•ผํ•œ๋‹ค๋ฉด ์œ„์™€ ๊ฐ™์ด ์ˆ˜์ •ํ•  ์ˆ˜ ์žˆ๋‹ค. ์˜ˆ์‹œ์—์„œ ํด๋ฆญ ์ด๋ฒคํŠธ์™€ ์กฐํšŒ์ˆ˜๋ฅผ ์ถ”๊ฐ€ํ–ˆ๋‹ค.

๋ฒŒ์จ๋ถ€ํ„ฐ ์‹ฌ์ƒ์น˜ ์•Š์€ ๋Š๋‚Œ์ด ๋“ ๋‹ค. ์ด๋ ‡๊ฒŒ ๊ตฌํ˜„ํ•œ๋‹ค๋ฉด props์˜ ๊ธธ์ด๊ฐ€ ์ ์  ๋Š˜์–ด๋‚˜ ์ฝ”๋“œ๊ฐ€ ๊ดด๋ฌผ์ด ๋˜์–ด๋ฒ„๋ฆด ๊ฒƒ ๊ฐ™๋‹ค.

์ง€๊ธˆ์€ ๊ทธ๋‚˜๋งˆ ๋‹จ์ˆœํ•œ ๊ตฌ์กฐ์ด์ง€๋งŒ, ํŠน์ • props ์กฐ๊ฑด์— ๋”ฐ๋ผ props๋“ค์˜ ์œ„์น˜๊ฐ€ ๋ฐ”๋€๋‹ค๋˜์ง€ ๊ทธ๋Ÿฐ ๋ถ€๋ถ„๋“ค์„ ์ƒ์ƒํ•ด๋ณด๋ฉด ๋”์ฐํ•œ ์ฝ”๋“œ๊ฐ€ ๋งŒ๋“ค์–ด์งˆ ๊ฒƒ์ด ๋ถ„๋ช…ํ•˜๋‹ค.

const Board = ({ onClick, children }) => {
  return (
    <div
      onClick={() => {
        onClick();
      }}
    >
      {children}
    </div>
  );
};

function App() {
  const { author, title, content, date, views, onClick } = {
    author: "lee",
    title: "My simple Board",
    content: "Hello this is my First Board",
    date: "today",

    views: 15,
    onClick: () => {
      alert(`Hello!`);
    },
  };
  return (
    <div className='App'>
      <Board onClick={onClick}>
        <div>์ž‘์„ฑ์ž: {author}</div>
        <div>์กฐํšŒ์ˆ˜: {views}</div>
        <div>
          ์ œ๋ชฉ: {title}, ๋‚ ์งœ {date}
        </div>
        <div>๋‚ด์šฉ: {content}</div>
      </Board>
    </div>
  );
}

์ด๋Ÿฐ ๊ตฌ์กฐ๋Š” ์–ด๋–จ๊นŒ? ์ด ์ฝ”๋“œ์™€ ์œ„์— ์žˆ๋Š” IOCCalulator์˜ ๊ตฌ์กฐ๋ฅผ ๋น„๊ตํ•ด๋ณด์ž. ๋น„์Šทํ•œ ๊ตฌ์กฐ๋ผ๊ณ  ๋Š๊ปด์ง„๋‹ค. IOCCalulator์ฒ˜๋Ÿผ Board ์ปดํฌ๋„ŒํŠธ๋Š” ๋ฐ›์€ children์„ ๋ Œ๋”๋งํ•  ๋ฟ, ๊ทธ๊ฒƒ์ด ๋ฌด์—‡์ธ์ง€๋Š” ๊ด€์‹ฌ์‚ฌ๊ฐ€ ์•„๋‹ˆ๋‹ค.

๋‹จ์ ?

ํ•˜์ง€๋งŒ ์ด๋Ÿฐ ๋ฐฉ๋ฒ•์€ ์ข‹์€ ์ ๋งŒ ์žˆ๋Š” ๊ฑด ์•„๋‹ˆ๋‹ค. ๋งŒ์•ฝ ๊ฐ™์€ Board์ธ๋ฐ author์™€ views๊ฐ€ ํ™€์ˆ˜ ๋ฒˆ ์งธ Board์—์„œ๋งŒ ์œ„์น˜๊ฐ€ ๋ฐ”๋€๋‹ค๊ณ  ์ƒ๊ฐํ•ด๋ณด์ž. ๋งค๋ฒˆ ์ƒํ™ฉ์— ๋”ฐ๋ผ children์œผ๋กœ ๋“ค์–ด๊ฐˆ ์š”์†Œ๋ฅผ ๋ฐ”๊ฟ”์ค˜์•ผํ•œ๋‹ค.

๋ฌผ๋ก  ์ด๋Ÿฐ ๊ฒƒ๋„ ์ถฉ๋ถ„ํ•œ ์ถ”์ƒํ™” ๊ณ„์ธต์„ ์ œ๊ณตํ•ด์„œ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ๋‹ค. OddBoardContent, EvenBoardContent ๋“ฑ์˜ ์ปดํฌ๋„ŒํŠธ๋ฅผ ๋งŒ๋“ค์–ด ์žฌ์‚ฌ์šฉํ•˜๋ฉด ๊ทธ๋งŒ์ด๋‹ค.

render props

const Counter = (props) => {
  const [count, setCount] = useState(0);

  return (
    <div onClick={() => setCount((prev) => prev + 1)}>
      props.children
    </div>
  );
};

render props ๋˜ํ•œ IoC ํŒจํ„ด์œผ๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. Counter ์ปดํฌ๋„ŒํŠธ์˜ count๋ฅผ children์— ์–ด๋–ป๊ฒŒ ์ „๋‹ฌํ• ๊นŒ? count๊ฐ€ ์—…๋ฐ์ดํŠธ๋  ๋•Œ๋งˆ๋‹ค children๋„ ๊ฐ™์ด ์—…๋ฐ์ดํŠธ ํ•ด๋ณด์ž.

const Counter = (props) => {
  const [count, setCount] = useState(0);

  return (
    <div onClick={() => setCount((prev) => prev + 1)}>
      {props.children(count)}
    </div>
  );
};

function App() {
  const { author } = {
    author: "lee",
  };

  return (
    <div className='App'>
      <Counter>
        {(count) => {
          return (
            <>
              <div>์ž‘์„ฑ์ž: {author}</div>
              <div>{count}</div>
            </>
          );
        }}
      </Counter>
    </div>
  );
}

children์— ํ•จ์ˆ˜๋ฅผ ์ „๋‹ฌํ•˜๊ณ  ๊ทธ๊ฒƒ์„ Counter ์ปดํฌ๋„ŒํŠธ๊ฐ€ ํ˜ธ์ถœํ•˜๊ณ  ์žˆ๋‹ค. ์ด๋Ÿฐ ํ˜•์‹์œผ๋กœ ์ž‘์„ฑํ•œ๋‹ค๋ฉด ๋งค state ์—…๋ฐ์ดํŠธ๋งˆ๋‹ค, children ํ•จ์ˆ˜๋ฅผ ์žฌ์‹คํ–‰ํ•œ๋‹ค.

๊ทธ๋Ÿฐ ๋งŒํผ children์˜ ๋ Œ๋”๋ง ๋น„์šฉ์ด ๋น„์‹ธ์ง€๋ฉด ์•ˆ ๋˜๊ธฐ ๋•Œ๋ฌธ์— ์ ์ ˆํ•˜๊ฒŒ ์ฝ”๋“œ๋ฅผ ๋‚˜๋ˆ„๊ฑฐ๋‚˜ ์‚ฌ์šฉ ์ž์ฒด์— ๋Œ€ํ•ด ๊ณ ๋ คํ•ด๋ด์•ผํ•œ๋‹ค.

ํ›„๊ธฐ

์ œ์–ด์˜ ์—ญ์ „ ๊ฐœ๋…์— ๋Œ€ํ•ด ์ดํ•ด๊ฐ€ ์ž˜ ๋˜์ง€ ์•Š์•„์„œ ๊ธ€์„ ์ž‘์„ฑํ•˜๋Š”๋ฐ ์˜ค๋žœ ์‹œ๊ฐ„์ด ๊ฑธ๋ ธ๋‹ค.

์‚ฌ์‹ค ์ด ํŒจํ„ด์€ ์•Œ๊ฒŒ ๋ชจ๋ฅด๊ฒŒ ๋งŽ์ด ์‚ฌ์šฉํ•˜๊ณ  ์žˆ๋‹ค. ๋‹น์žฅ ๋ฆฌ์•กํŠธ์˜ ์ „์ฒด์ ์ธ ์ž‘๋™๋งŒ ๋ด๋„ ์ œ์–ด์˜ ์—ญ์ „์ด๋‹ค. ์šฐ๋ฆฌ๋Š” ๋‹จ์ˆœํžˆ JSX์™€ ๋กœ์ง๋“ค์„ ์ž‘์„ฑํ•  ๋ฟ์ด์ง€๋งŒ, ๋ฆฌ์•กํŠธ๊ฐ€ ์ œ๊ณตํ•˜๋Š” ๊ธฐ๋Šฅ๋“ค์„ ๋ณด์ž.

V-DOM, ๋ฆฌ๋ Œ๋”๋ง, props ๋“ฑ ๋ฆฌ์•กํŠธ ๋‚ด๋ถ€์—์„œ ๋””์ž์ธ ๋œ ๊ธฐ๋Šฅ๋“ค์ด๋‹ค. ๊ทธ๊ฒƒ์ด ์–ด๋–ป๊ฒŒ ๋™์ž‘ํ•˜๋Š”์ง€๋Š” ์šฐ๋ฆฌ์˜ ๊ด€์‹ฌ์‚ฌ๊ฐ€ ์•„๋‹ˆ๋‹ค. ์ œ์–ด์˜ ์—ญ์ „์€ ์กฐ๊ธˆ ๋” ์œ ์—ฐํ•œ ์ถ”์ƒํ™”์ธ๊ฐ€?

๋ˆ„๊ตฐ๊ฐ€ ๋‚˜์—๊ฒŒ ์ œ์–ด์˜ ์—ญ์ „์— ๋Œ€ํ•ด ๋ฌป๋Š”๋‹ค๋ฉด ๋Œ€๋‹ต์€ ํ•  ์ˆ˜ ์žˆ๊ฒ ์ง€๋งŒ, ๋‚ด ๋ˆˆ๋น›์€ ํ™•์‹ ์— ์ฐจ์žˆ์ง€ ๋ชปํ•  ๊ฒƒ ๊ฐ™๋‹ค.

https://kentcdodds.com/blog/inversion-of-control
https://www.lorenzweiss.de/inversion_of_control_with_react_components/
https://brunch.co.kr/@finda/556

์œ„ ๋ฌธ์„œ๋“ค์„ ์ฐธ๊ณ ํ–ˆ์Šต๋‹ˆ๋‹ค.

profile
ํ”„๋ก ํŠธ์—”๋“œ ๊ฐœ๋ฐœ์ž๋กœ ๊ฑธ์–ด๊ฐ€๋Š” ์ค‘์ž…๋‹ˆ๋‹ค.

0๊ฐœ์˜ ๋Œ“๊ธ€