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

[ ๋ฆฌ์•กํŠธ(React) ] ๋ถˆํ•„์š”ํ•œ prop drilling ์ œ๊ฑฐํ•˜๊ธฐ

by YWTechIT 2022. 7. 27.
728x90

๐Ÿ“ ๋ถˆํ•„์š”ํ•œ prop drilling ์ œ๊ฑฐํ•˜๊ธฐ

React์—์„œ ํŽ˜์ด์ง€ ๋‚ด๋ถ€์— 2~3๊ฐœ์˜ ์ปดํฌ๋„ŒํŠธ๋ฅผ ์„ ์–ธํ•˜๋ฉฐ ๋ฐ์ดํ„ฐ๋ฅผ props๋กœ ์ „๋‹ฌํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ์žˆ์„ ๊ฒƒ์ด๋‹ค. ์ด๋ฒˆ ๊ธ€์€ ํ•œ ํŽ˜์ด์ง€ ๋‚ด์— ์ปดํฌ๋„ŒํŠธ 3๊ฐœ(A -> B -> C)๋ฅผ ์„ ์–ธํ•˜์—ฌ ๋‹จ์ˆœํžˆ props๋กœ ์ „๋‹ฌ๋งŒํ•˜๋Š” B์ปดํฌ๋„ŒํŠธ์˜ prop drilling์„ ์–ด๋–ป๊ฒŒ ํ•ด๊ฒฐํ–ˆ๋Š”์ง€ ์•Œ์•„๋ณด์ž.

 

์šฐ์„ , React๋ฅผ ๋‹ค๋ค„ ๋ณธ ๊ฐœ๋ฐœ์ž๋ผ๋ฉด prop drilling์— ๋Œ€ํ•ด ํ•œ ๋ฒˆ์ฏค์€ ๋“ค์–ด๋ดค์„ ๊ฒƒ์ด๋‹ค. prop drilling์ด๋ž€, props๋ฅผ ์ด์šฉํ•ด ์ž์‹ ์ปดํฌ๋„ŒํŠธ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ๋‚ด๋ ค์ค„ ๋•Œ ๋ชจ๋“  ๋ ˆ๋ฒจ์—์„œ ๊ฐ™์€ ๋ฐ์ดํ„ฐ๊ฐ€ ์ „์†ก๋˜๋Š” ์ƒํ™ฉ์„ ๋งํ•œ๋‹ค.

 

 

๊ทœ๋ชจ๊ฐ€ ์ž‘์„ ๋•Œ prop drilling์€ ๋น ๋ฅด๊ณ  ์‰ฝ๊ฒŒ ๋ฐ์ดํ„ฐ๋ฅผ ์ „์†กํ•  ์ˆ˜ ์žˆ๊ณ , ๊ตฌํ˜„ ๋ฐฉ๋ฒ•์ด ์‰ฝ๊ณ , props๋กœ ์ „๋‹ฌ๋œ ๋ฐ์ดํ„ฐ์˜ ์ƒํƒœ ๋ณ€๊ฒฝ ์‹œ ์ƒˆ๋กœ์šด ๋ณ€๊ฒฝ์‚ฌํ•ญ์„ ์‰ฝ๊ฒŒ ์—…๋ฐ์ดํŠธ ํ•  ์ˆ˜ ์žˆ๋Š” ์žฅ์ ์ด ์žˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ์ž์‹ ์ปดํฌ๋„ŒํŠธ๊ฐ€ ๋ถ€๋ชจ ์ปดํฌ๋„ŒํŠธ์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์ „๋‹ฌํ•ด์ฃผ๋Š” ์—ญํ• ๋กœ๋งŒ ์‚ฌ์šฉ๋˜๊ณ  ๊ทธ ๋•Œ๋ฌธ์— ์ฝ”๋“œ๊ฐ€ ๋ถˆํ•„์š”ํ•˜๊ฒŒ ๊ธธ์–ด์ง€๋ฉฐ ํ•„์š”ํ•˜์ง€ ์•Š์€ ๋ฐ์ดํ„ฐ๊นŒ์ง€ ์ „๋‹ฌํ•ด์ค„ ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ํ”„๋กœ์ ํŠธ์˜ ๊ทœ๋ชจ๊ฐ€ ์ปค์งˆ์ˆ˜๋ก ๊ถŒ์žฅํ•˜์ง€ ์•Š๋Š” ๋ฐฉ๋ฒ•์ด๋‹ค. ์ด๋Ÿด ๊ฒฝ์šฐ contextAPI ํ˜น์€ ์ „์—ญ์ƒํƒœ๊ด€๋ฆฌ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์˜ ๋„์›€์„ ๋ฐ›์„ ์ˆ˜ ์žˆ์ง€๋งŒ, ์ด๋ฒˆ์—” ๊ทธ๋“ค์˜ ๋„์›€์„ ๋ฐ›์ง€ ์•Š๊ณ  ์ฝ”๋“œ๋ฅผ ์–ด๋–ป๊ฒŒ ์ค„์˜€๋Š”์ง€๋ฅผ ์˜ˆ์‹œ๋ฅผ ๋“ค์–ด ์‚ดํŽด๋ณด์ž.

 

728x90

 

์šฐ์„ , API๋ฅผ ์ด์šฉํ•ด User๊ฐ€ ๊ฐ€์ž…ํ•œ ๊ณ„์ •์„ ์ฐพ๋Š” Accounts ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ ธ์˜จ๋‹ค๊ณ  ๊ฐ€์ •ํ•ด๋ณด์ž. ํ•„์ž๋Š” ์ฒ˜์Œ AccountsFound๋ž€ ์ปดํฌ๋„ŒํŠธ์—์„œ User์˜ Accounts๋ฅผ ๊ฐ€์ ธ์˜จ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๊ฐ™์€ ๋ฐ์ดํ„ฐ์˜ Accounts๊ฐ€ ๋‹ด๊ฒจ์žˆ๋Š” ๋ฐ์ดํ„ฐ๋ฅผ Accounts๋ž€ ์ปดํฌ๋„ŒํŠธ๋กœ ๋„˜๊ธฐ๊ณ  ์ด๋“ค์„ ํ•˜๋‚˜์”ฉ ๋ณด์—ฌ์ฃผ๊ธฐ ์œ„ํ•ด mapํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ด Account๋ผ๋Š” ์ปดํฌ๋„ŒํŠธ๋กœ ์ „๋‹ฌํ•ด์ฃผ์—ˆ๋‹ค. ์•„๋ž˜ ์ฝ”๋“œ๋ธ”๋ก์„ ๋ณด์ž.

// before
interface AccountType {
  name: string
  email: string 
  createdAt: Date
}

function AccountsFound({ accounts }: { accounts: AccountType[] }) {
  return <Accounts accounts={accounts} />
}

function Accounts({ accounts }: { accounts: AccountType[] }) {
  return (
    <>
      <div>๊ณ ๊ฐ๋‹˜์˜ ๊ณ„์ • ์ •๋ณด์ž…๋‹ˆ๋‹ค.</div>
      {accounts.map((item, idx) => (
        <Account key={idx} info={item} />
      ))}
    </>
  )
}

function Account({ account }: { account: AccountType }) {
  const { name, email, createdAt } = extractAccountData(account)

  return (
    <>
      <div>{name}</div>
      <div>{email}</div>
      <div>{createdAt}</div>
    </>
  )
}

์ด ํŽ˜์ด์ง€์—์„œ ์ž‘์„ฑ๋œ ์ฝ”๋“œ ์ค‘ ๋น„ํšจ์œจ์ ์œผ๋กœ ์ž‘์„ฑ๋œ ๋ถ€๋ถ„์„ ์ฐพ์„ ์ˆ˜ ์žˆ๊ฒ ๋Š”๊ฐ€?

  1. 15๋ผ์ธ์˜ <div>๋Š” map์„ ํ†ตํ•ด accounts์˜ ๊ฐœ์ˆ˜๋งŒํผ ์ƒˆ๋กญ๊ฒŒ ๋ Œ๋”๋ง ๋œ๋‹ค. ๊ฐ™์€ ๋ฐ์ดํ„ฐ๋ฅผ ๋ถˆํ•„์š”ํ•˜๊ฒŒ ๋งค๋ฒˆ ์ƒˆ๋กญ๊ฒŒ ๋ Œ๋”๋ง ๋œ๋‹ค.
  2. ๋ถ€๋ชจ(<Accounts />) ์ปดํฌ๋„ŒํŠธ๋Š” ์ž์‹(<Account />) ์ปดํฌ๋„ŒํŠธ์—๊ฒŒ ๋‹จ์ˆœํžˆ ๋ฐ์ดํ„ฐ๋งŒ ์ „๋‹ฌํ•ด์ฃผ๋Š” ์—ญํ• ๋งŒ ํ•˜๊ณ  ์žˆ๋‹ค. (์ด ๋ถ€๋ถ„์ด propDrilling) ๋”ฐ๋ผ์„œ, <Accounts /> ์ปดํฌ๋„ŒํŠธ๋ฅผ ์—†์•ค๋‹ค. ์ฆ‰, ์ปดํฌ๋„ŒํŠธ์˜ ๋ Œ๋”๋ง ์ˆœ์„œ๋ฅผ AccountsFound -> Accounts -> Account์—์„œ AccountsFound -> Account๋กœ ์ค„์—ฌ์ค€๋‹ค.
// after
interface AccountType {
  name: string
  email: string 
  createdAt: Date
}

function AccountsFound({ accounts }: { accounts: AccountType[] }) {
  return (
    <>
      <div>๊ณ ๊ฐ๋‹˜์˜ ๊ณ„์ • ์ •๋ณด์ž…๋‹ˆ๋‹ค.</div>
      {accounts.map((account, idx) => (
        <Account key={idx} account={account} />
      ))}
    </>
  )
}

function Account({ account }: { account: AccountType }) {
  const { name, email, createdAt } = extractAccountData(account)

  return (
    <>
      <div>{name}</div>
      <div>{email}</div>
      <div>{createdAt}</div>
    </>
  )
}

์ด๋ ‡๊ฒŒ ํ•ด์„œ prop drilling์„ ์ „์—ญ์ƒํƒœ๊ด€๋ฆฌ๋กœ ํ•ด๊ฒฐํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ๋ถˆํ•„์š”ํ•œ ์ปดํฌ๋„ŒํŠธ๋ฅผ ์ œ๊ฑฐํ•˜๋ฉด์„œ ์ฝ”๋“œ๋ฅผ ๊ฐ„๋‹จํžˆ ์ค„์ด๋Š” ์˜ˆ์‹œ๋ฅผ ์‚ดํŽด๋ดค๋‹ค. ๋‹จ์ˆœํžˆ ๋ฐ์ดํ„ฐ๋งŒ ์ „๋‹ฌํ•ด์ฃผ๋Š” ์ปดํฌ๋„ŒํŠธ๊ฐ€ ๋งŽ์•„์กŒ๋‹ค๊ณ  ์ƒ๊ฐํ•˜๋ฉด prop drilling์„ ํ•˜๋Š” ๊ฒƒ์€ ์•„๋‹Œ์ง€ ํ•œ ๋ฒˆ์ฏค ์ž์‹ ์„ ๋˜๋Œ์•„๋ณผ ์ˆ˜ ์žˆ๋Š” ์‹œ๊ฐ„์„ ์ž ๊น์ด๋‚˜๋งˆ ๊ฐ€์ง€๋ฉฐ ์ด ๊ธ€์„ ๋งˆ์นœ๋‹ค.

 

Reference

  1. https://www.geeksforgeeks.org/what-is-prop-drilling-and-how-to-avoid-it/
๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€