๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
React

[React] React Children Props์˜ ๊ธฐ๋ฌ˜ํ•œ ๋ Œ๋”๋ง ์ตœ์ ํ™” ํ˜„์ƒ์˜ ์›์ธ

by LasBe 2023. 11. 1.
๋ฐ˜์‘ํ˜•

๐Ÿ“’  React์˜ children prop

๋ฆฌ์•กํŠธ์—์„œ๋Š” ๋™์ ์œผ๋กœ ์ž์‹ ์ปดํฌ๋„ŒํŠธ๋ฅผ ๋ Œ๋”๋ง ํ•˜๊ธฐ ์œ„ํ•ด ๋ถ€๋ชจ ์ปดํฌ๋„ŒํŠธ์—์„œ children prop์„ ์ด์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

๋ชจ๋‘๊ฐ€ ์ž˜ ์•Œ๊ณ  ์žˆ๋“ฏ ์‚ฌ์šฉ๋ฒ•์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

const Children = () => {
  return <div>Children</div>;
};

const Parent = ({ children }: { children: JSX.Element }) => {
  return <div>{children}</div>;
};

export const Container = () => {
  return (
    <div>
      <Parent>
        <Children />
      </Parent>
    </div>
  );
}

์ด๋Ÿฌํ•œ ์‚ฌ์šฉ ๋ฐฉ์‹์€ React Context์˜ Provider, BrowserRouter, RecoilRoot๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ๋„ ๋™์ผํ•ฉ๋‹ˆ๋‹ค.

 

์—ฌ๊ธฐ๊นŒ์ง„ ๋Š˜ ์•Œ๊ณ  ์žˆ์—ˆ๋˜ ๋‚ด์šฉ์ž…๋‹ˆ๋‹ค.


๐Ÿ“Œ Children Prop์— ๋Œ€ํ•œ ์˜๊ตฌ์‹ฌ

์ €๋Š” Context๋ฅผ ์ด์šฉํ•ด ๋ชจ๋‹ฌ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ๋งŒ๋“ค๋˜ ๋„์ค‘

Context๋ฅผ ๊ฐ’๊ณผ ๊ฐ’์„ ์„ค์ •ํ•˜๋Š” Context ๋‘ ๊ฐœ๋กœ ๋ถ„๋ฆฌํ•˜๋Š”๊ฒŒ ์ตœ์ ํ™”์— ์ด์ ์ด ์žˆ๋‹ค๋Š” ๊ฒƒ์„ ๋ณด๊ณ 

๋‹ค์Œ๊ณผ ๊ฐ™์ด Provider ์ปดํฌ๋„ŒํŠธ๋ฅผ ๋งŒ๋“ค๋˜ ๋„์ค‘ ๋ฌธ๋“ ์˜๊ตฌ์‹ฌ์ด ๋“ค์—ˆ์Šต๋‹ˆ๋‹ค.

<ValueContext.Provider value={...}>
  <SetContext.Provider value={...}>
    {children}
  </SetContext.Provider>
</ValueContext.Provider>

๋ถ„๋ช…ํžˆ ๋ฆฌ์•กํŠธ์˜ ๋ฆฌ๋ Œ๋”๋ง ์กฐ๊ฑด์—” ๋ถ€๋ชจ ์ปดํฌ๋„ŒํŠธ๊ฐ€ ๋ฆฌ๋ Œ๋”๋ง ๋˜๋ฉด ์ž์‹ ์ปดํฌ๋„ŒํŠธ๋„ ๋ฆฌ๋ Œ๋”๋ง ๋œ๋‹ค๊ณ  ์•Œ๊ณ  ์žˆ์—ˆ๋Š”๋ฐ,

ValueProvider์˜ value๊ฐ€ ๋ฐ”๋€Œ๋ฉด ๊ทธ ์ž์‹ ์ปดํฌ๋„ŒํŠธ์ธ SetContext, children ๋˜ํ•œ ๊ฐ™์ด ๋ฆฌ๋ Œ๋”๋ง ๋˜๋Š”๊ฑฐ ์•„๋‹Œ๊ฐ€???

๋ผ๋Š” ์ƒ๊ฐ์— ๊ฝ‚ํ˜€ ๋‹ค ์ œ์ณ๋‘๊ณ  children prop์˜ ์ž‘๋™ ๋ฐฉ์‹์— ๋Œ€ํ•ด ํŒŒ๊ณ ๋“ค๊ธฐ ์‹œ์ž‘ํ–ˆ์Šต๋‹ˆ๋‹ค.


๐Ÿ“Œ Children Prop์˜ ์ตœ์ ํ™” ํ˜„์ƒ

์œ„ ์˜ˆ์ œ์—์„œ ์กฐ๊ธˆ ์ˆ˜์ •ํ•œ ์ฝ”๋“œ์ž…๋‹ˆ๋‹ค.

import { useState } from "react";

const Children = () => {
  console.log("Children ์ปดํฌ๋„ŒํŠธ ๋ฆฌ๋ Œ๋”๋ง");
  return <div>Children</div>;
};

const Parent = ({ children }: { children: JSX.Element }) => {
  const [state, setState] = useState(false);
  const handleClickButton = () => setState(!state);
  console.log("Parent ์ปดํฌ๋„ŒํŠธ ๋ฆฌ๋ Œ๋”๋ง");
  return (
    <div>
      <button onClick={handleClickButton}>Parent ์ƒํƒœ๋ณ€ํ™”</button>
      {children}
    </div>
  );
};

export const Container = () => {
  return (
    <div>
      <Parent>
        <Children />
      </Parent>
    </div>
  );
};

 

์œ„ ๋ฒ„ํŠผ์„ ๋ˆŒ๋Ÿฌ Parent ์ปดํฌ๋„ŒํŠธ์— ๋ฆฌ๋ Œ๋”๋ง์„ ๋ฐœ์ƒ์‹œํ‚ค๋ฉด Children ์ปดํฌ๋„ŒํŠธ๋Š” ์–ด๋–ป๊ฒŒ ๋ ๊นŒ์š”?

๋†€๋ž๊ฒŒ๋„ Children ์ปดํฌ๋„ŒํŠธ๋Š” React.memo๋ฅผ ์‚ฌ์šฉํ•ด ๋ฉ”๋ชจ์ด์ œ์ด์…˜ ํ•œ ๊ฒƒ ์ฒ˜๋Ÿผ ๋ฆฌ๋ Œ๋”๋ง์ด ๋ฐœ์ƒํ•˜์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ค!

 

์–ด๋–ป๊ฒŒ ๋ถ€๋ชจ ์ปดํฌ๋„ŒํŠธ์ธ Parent๊ฐ€ ๋ฆฌ๋ Œ๋”๋ง ๋˜์—ˆ๋Š”๋ฐ ์ž์‹์ธ Children ์ปดํฌ๋„ŒํŠธ๋Š” ๋ฆฌ๋ Œ๋” ๋˜์ง€ ์•Š์•˜์„๊นŒ์š”?


๐Ÿ“Œ Children Prop, ์ตœ์ ํ™” ํ˜„์ƒ์˜ ์ด์œ 

์œ„ ์˜ˆ์ œ์—์„œ Container ์ปดํฌ๋„ŒํŠธ๋งŒ ๊ฐ€์ ธ์™”์Šต๋‹ˆ๋‹ค.

export const Container = () => {
  return (
    <div>
      <Parent>
        <Children />
      </Parent>
    </div>
  );
};

 

์ด ์ปดํฌ๋„ŒํŠธ์—์„œ Parent ์ปดํฌ๋„ŒํŠธ์˜ children prop์„ ๋‹ค๋ฅธ ๋ฐฉ์‹์œผ๋กœ ํ‘œํ˜„ํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

<div>
  <Parent children={Children()} />
</div>

์ด๋ ‡๊ฒŒ ํ•จ์ˆ˜ ํ˜•ํƒœ๋กœ ๋ณด๋‹ˆ ๋ถ€๋ชจ ์ž์‹ ๊ด€๊ณ„์™€ JSX ํƒœ๊ทธ์˜ ํ˜•ํƒœ์— ๋Œ€ํ•ด์„œ๋งŒ ๋งค๋ชฐ๋˜์–ด ๋ณด์ด์ง€ ์•Š๋˜ ๊ฒƒ๋“ค์ด ๋ณด์ด๊ธฐ ์‹œ์ž‘ํ•ฉ๋‹ˆ๋‹ค.

 

์‚ฌ์‹ค children prop์—๋Š” ๊ทธ์ € ํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด ๋ฐ˜ํ™˜ ๋œ ๊ฐ์ฒด๋ฅผ ๋„ฃ์–ด์ฃผ์—ˆ์„ ๋ฟ์ด๊ณ ,
์ด ๊ฐ์ฒด๋Š” Container ์ปดํฌ๋„ŒํŠธ์—์„œ ํ˜ธ์ถœ๋œ ๊ฒƒ์ด๋ผ๋Š” ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๊ทธ๋Ÿฌ๋‹ˆ๊น children์ด๋ž€ ๋ง์— ์†์•„ ์œ„์™€ ๊ฐ™์€ ๊ณ„์ธต ๊ตฌ์กฐ๋กœ ์ƒ๊ฐํ•  ์ˆ˜ ์žˆ์ง€๋งŒ
์‹ค์ œ๋กœ๋Š” ์•„๋ž˜์™€ ๊ฐ™์€ ๊ณ„์ธต ๊ตฌ์กฐ๋กœ ๋˜์–ด ์žˆ๋Š” ๊ฒƒ์ด์ฃ .

๊ทธ๋Ÿฌ๋‹ˆ Children ์ปดํฌ๋„ŒํŠธ๊ฐ€ ๋ฆฌ๋ Œ๋”๋ง ๋˜๋ ค๋ฉด Parent ์ปดํฌ๋„ŒํŠธ๊ฐ€ ์•„๋‹ˆ๋ผ

์‹ค์ œ ๋ถ€๋ชจ์ธ Container ์ปดํฌ๋„ŒํŠธ๊ฐ€ ๋ฆฌ๋ Œ๋”๋ง ๋˜์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

 

๊ทธ๋Ÿผ ์ด ์›๋ฆฌ๋ฅผ ์ด์šฉํ•ด ๋ฆฌ๋ Œ๋”๋ง์ด ์ ์€ Container์— children props๋ฅผ ์ด์šฉํ•˜๋Š” ์ปดํฌ๋„ŒํŠธ๋ฅผ ์ž˜๋งŒ ์ด์šฉํ•œ๋‹ค๋ฉด

๋ฉ”๋ชจ์ด์ œ์ด์…˜๊ณผ ์œ ์‚ฌํ•œ ๋ Œ๋”๋ง ์ตœ์ ํ™” ํ˜„์ƒ์„ ์œ ๋„ํ•  ์ˆ˜ ์žˆ์„ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.

๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€


์˜คํ”ˆ ์ฑ„ํŒ