[React] Props Drilling

HP :) ๐Ÿ˜ƒยท2022๋…„ 7์›” 4์ผ
0
post-thumbnail
post-custom-banner

๐Ÿ“š ๊ฐœ๋…

Props Drilling์ด๋ž€ props๋ฅผ ํ†ตํ•ด ํ•˜์œ„ ์ปดํฌ๋„ŒํŠธ๋กœ data๋ฅผ ๋„˜๊ฒจ์ค„๋•Œ 3~5๊ฐœ ์ •๋„์˜ ์ปดํฌ๋„ŒํŠธ๋ผ๋ฉด ๊ด€๋ฆฌํ•˜๊ธฐ์— ์–ด๋ ต์ง€ ์•Š์ง€๋งŒ 10๊ฐœ 20๊ฐœ 30๊ฐœ .. ์ „๋‹ฌํ•ด์•ผํ•˜๋Š” ์ปดํฌ๋„ŒํŠธ๋“ค์˜ ๊ฐฏ์ˆ˜๊ฐ€ ๋งŽ์•„์งˆ์ˆ˜๋ก ์œ ์ง€๋ณด์ˆ˜ํ• ๋•Œ์˜ ๋ฌธ์ œ๊ฐ€ ์ƒ๊ธฐ๋Š” ๊ฒƒ์„ ๋งํ•ฉ๋‹ˆ๋‹ค.

์•„๋ž˜ ์˜ˆ์‹œ๋ฅผ ํ†ตํ•ด Props Drilling์— ๋Œ€ํ•ด ์ž์„ธํžˆ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

์˜ˆ์‹œ

import React from 'react';

function App() {
  const data = `๋“œ๋””์–ด ๋„์ฐฉ`;
  return (
    <div>
      <One data={data} />
    </div>
  );
}

function One({ data }) {
  return (
    <div>
      <h3>์ฒซ๋ฒˆ์งธ ์ปดํฌ๋„ŒํŠธ ์ž…๋‹ˆ๋‹ค.</h3>
      <Two data={data} />
    </div>
  );
}

function Two({ data }) {
  return (
    <div>
      <h3>๋‘๋ฒˆ์งธ ์ปดํฌ๋„ŒํŠธ ์ž…๋‹ˆ๋‹ค.</h3>
      <Three data={data} />
    </div>
  );
}

function Three({ data }) {
  return (
    <div>
      <h3>์„ธ๋ฒˆ์งธ ์ปดํฌ๋„ŒํŠธ ์ž…๋‹ˆ๋‹ค.</h3>
      <Four data={data} />
    </div>
  );
}

function Four({ data }) {
  return (
    <div>
      <h3>๋„ค๋ฒˆ์งธ ์ปดํฌ๋„ŒํŠธ ์ž…๋‹ˆ๋‹ค.</h3>
      <ShowProps data={data} />
    </div>
  );
}

function ShowProps({ data }) {
  return <h3>{data}</h3>;
}

export default App;

์œ„์™€ ๊ฐ™์ด data๋ผ๋Š” props๋ฅผ ์ตœํ•˜๋‹จ์˜ ์ž์‹ ์ปดํฌ๋„ŒํŠธ๊ฐ€ ๋ฐ์ดํ„ฐ๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด ์ „๋‹ฌํ•˜๊ณ  ์žˆ๊ณ  ์ด๋•Œ ์ƒ์œ„ ๋ถ€๋ชจ์™€ ์ž์‹ ์ปดํฌ๋„ŒํŠธ ์‚ฌ์ด์˜ ๊นŠ์ด๊ฐ€ ์ปค์ ธ ๊ด€๋ฆฌํ•˜๋Š”๊ฒƒ์— ๋Œ€ํ•œ ์–ด๋ ค์›€์ด ์žˆ์Šต๋‹ˆ๋‹ค.

โš’๏ธ ํ•ด๊ฒฐ ๋ฐฉ์•ˆ

์œ„์™€ ๊ฐ™์€ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ์—ฌ๋Ÿฌ ๋ฐฉ๋ฒ•๋“ค์ด ์กด์žฌํ•˜๋Š”๋ฐ ๋Œ€ํ‘œ์ ์œผ๋กœ๋Š” ์ƒํƒœ๊ด€๋ฆฌ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ ์ž…๋‹ˆ๋‹ค. ๋Œ€ํ‘œ์ ์œผ๋กœ๋Š” redux , MobX ๋“ฑ์ด ์กด์žฌํ•ฉ๋‹ˆ๋‹ค. ๋˜ํ•œ useContext๋ผ๋Š” React Hook์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š”๋ฐ ์šฐ๋ฆฌ๋Š” useContext๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์œ„์— ์ฝ”๋“œ๋ฅผ ๋ฆฌํŒฉํ† ๋ง ํ•ด๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

import React, { useContext } from 'react';
import { DataContext } from './hooks/DataContext';

function App() {
  const data = `๋“œ๋””์–ด ๋„์ฐฉ`;
  return (
    <DataContext.Provider value={data}>
      <One />
    </DataContext.Provider>
  );
}

function One() {
  return (
    <div>
      <h3>์ฒซ๋ฒˆ์งธ ์ปดํฌ๋„ŒํŠธ ์ž…๋‹ˆ๋‹ค.</h3>
      <Two />
    </div>
  );
}

function Two() {
  return (
    <div>
      <h3>๋‘๋ฒˆ์งธ ์ปดํฌ๋„ŒํŠธ ์ž…๋‹ˆ๋‹ค.</h3>
      <Three />
    </div>
  );
}

function Three() {
  return (
    <div>
      <h3>์„ธ๋ฒˆ์งธ ์ปดํฌ๋„ŒํŠธ ์ž…๋‹ˆ๋‹ค.</h3>
      <Four />
    </div>
  );
}

function Four() {
  return (
    <div>
      <h3>๋„ค๋ฒˆ์งธ ์ปดํฌ๋„ŒํŠธ ์ž…๋‹ˆ๋‹ค.</h3>
      <ShowProps />
    </div>
  );
}

function ShowProps() {
  const data = useContext(DataContext);
  return <h3>{data}</h3>;
}

export default App;

โ—๏ธ ์ฃผ์˜ํ•  ์ 

๋‹ค๋งŒ useContext๋ฅผ ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ์—๋Š” ์ปดํฌ๋„ŒํŠธ๋ฅผ ์žฌ์‚ฌ์šฉํ•˜๊ธฐ ์–ด๋ ค์›Œ ์งˆ ์ˆ˜ ์žˆ์–ด์„œ ๊ผญ ํ•„์š”ํ• ๋•Œ๋งŒ ์‚ฌ์šฉํ•ด์•ผ ํ•˜๊ณ  ๋งŒ์ผ Props drilling์„ ํ”ผํ•˜๊ธฐ ์œ„ํ•œ ๋ชฉ์ ์ด๋ผ๋ฉด Component Composition ( ์ปดํฌ๋„ŒํŠธ ํ•ฉ์„ฑ )์„ ๋จผ์ € ๊ณ ๋ คํ•ด์•ผ ํ•œ๋‹ค.

๐Ÿ“Œ ์ฐธ๊ณ 

Props Drilling
๋ณ„์ฝ”๋”ฉ๋‹˜์˜ useContext ์œ ํŠœ๋ธŒ ๊ฐ•์˜

profile
๋Š์ž„์—†์ด ๋…ธ๋ ฅํ•˜๋Š” ๊ฐœ๋ฐœ์ž
post-custom-banner

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