[React] ๋ฆฌ์•กํŠธ Props Drilling

yayaya_hojinยท2023๋…„ 9์›” 4์ผ
0

๋ฆฌ์—‘ํŠธ

๋ชฉ๋ก ๋ณด๊ธฐ
3/6
post-thumbnail

๐Ÿš€ React์—์„œ๋Š” Props Drilling์œผ๋กœ ์ปดํฌ๋„ŒํŠธ ๊ฐ„์— ๋ฐ์ดํ„ฐ๋ฅผ ์ „๋‹ฌํ•œ๋‹ค.

Props Drilling

Props Drilling์€ props๋ฅผ ์ปดํฌ๋„ŒํŠธ ๊ฐ„์— ๋ฐ์ดํ„ฐ๋ฅผ ์ „๋‹ฌํ•˜๋Š” ๊ณผ์ •์ด๋‹ค.

์ƒ์œ„ ์ปดํฌ๋„ŒํŠธ์—์„œ ํ•˜์œ„ ์ปดํฌ๋„ŒํŠธ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์ „๋‹ฌํ•˜๋Š” ๊ณผ์ •์—์„œ ๋‚˜ํƒ€๋‚ด๊ฒŒ ๋œ๋‹ค.

์•„๋ž˜ ๊ทธ๋ฆผ๊ณผ ๊ฐ™์€ ํŠธ๋ฆฌ ๊ตฌ์กฐ๋ฅผ ๋‚˜ํƒ€๋‚ธ๋‹ค.

props drilling ํŠธ๋ฆฌ

ComponentA.js

import React from "react";

const ComponentA = () => {
  return <ComponentC text="Hello world!" />;
};

const ComponentC = (props) => {
  return <ComponentE text={props.text} />;
};

const ComponentE = (props) => {
  return <div>{props.text}</div>;
};

export default ComponentA;

๐ŸŒŸ Component A์—์„œ Component E๋กœ ๋ฐ์ดํ„ฐ ์ „๋‹ฌ ๊ณผ์ •

Component A์—์„œ Component E๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์ „๋‹ฌํ•˜๋ ค๋ฉด ์ค‘๊ฐ„์— ์žˆ๋Š” Component C๋กœ props๋ฅผ ์ „๋‹ฌํ•˜๊ณ  ๊ทธ ํ›„ Component C์—์„œ ๋ฐ›์€ props๋ฅผ ์ตœ์ข… Component E๋กœ ์ „๋‹ฌํ•œ๋‹ค.

๋ถ€๋ชจ โ†’ ์ค‘๊ฐ„ โ†’ ... โ†’ ์ตœํ•˜์œ„


Props Drilling ์žฅ์ 

๐Ÿ‘ Props๋ฅผ ์‚ฌ์šฉํ•ด ์ง๊ด€์ ์ด๊ณ  ๊ฐ„๋‹จํ•จ

๐Ÿ‘ ๋ณ„๋„์— ์ƒํƒœ๊ด€๋ฆฌ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ๋ฐ ์ปจํ…์ŠคํŠธ๋ฅผ ๋„์ž…ํ•˜์ง€ ์•Š๊ณ  ๋ฐ์ดํ„ฐ ์ „๋‹ฌ ๊ฐ€๋Šฅ

๐Ÿ‘ ์ปดํฌ๋„ŒํŠธ ์žฌ์‚ฌ์šฉ์„ฑ ํ–ฅ์ƒ

๐Ÿ‘ ์ƒ์œ„ ์ปดํฌ๋„ŒํŠธ์—์„œ ๋ฐ์ดํ„ฐ ๋ณ€๊ฒฝ ์‹œ ํ•˜์œ„ ์ปดํฌ๋„ŒํŠธ๋„ ๋™์ผํ•˜๊ฒŒ ๋ฐ์ดํ„ฐ ์ˆ˜์‹  ๋ฐ ์ฒ˜๋ฆฌ ๊ฐ€๋Šฅ

๐Ÿ‘ ํŠธ๋ฆฌ ํ˜•ํƒœ์˜ ๊ณ„์ธต ๊ตฌ์กฐ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ์–ด ์ปดํฌ๋„ŒํŠธ ๊ฐ„์˜ ๊ด€๊ณ„๊ฐ€ ๋ช…ํ™•ํ•จ


Props Drilling ๋‹จ์ 

๐Ÿ‘Ž ํ•˜์œ„ ์ปดํฌ๋„ŒํŠธ์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด ์ค‘๊ฐ„ ์ปดํฌ๋„ŒํŠธ์—์„œ ์‚ฌ์šฉํ•˜์ง€ ์•Š์•„๋„ ๊ฑฐ์ณ๊ฐ€์•ผ ๋˜๋Š” ๋ถˆํ•„์š”ํ•œ ์ „๋‹ฌ ๋ฌธ์ œ

๐Ÿ‘Ž ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ๊ทœ๋ชจ๊ฐ€ ์ปค์งˆ์ˆ˜๋ก props ์ „๋‹ฌ ๊ณผ์ •์ด ๋งŽ์•„์ง€๋Š” ์ถ”์  ๋ฐ ์„ฑ๋Šฅ ์ €ํ•˜ ๋ฌธ์ œ

๐Ÿ‘Ž ๋ชจ๋“  ํ•˜์œ„ ์ปดํฌ๋„ŒํŠธ๋Š” ์ƒ์œ„ ์ปดํฌ๋„ŒํŠธ๋ฅผ ๊ฑฐ์ณ์„œ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ›์•„์•ผ ํ•˜๋Š” ์˜์กด์„ฑ ๋ฌธ์ œ

๐Ÿ‘Ž ์ตœ์ƒ์œ„ ๋ถ€๋ชจ ์ปดํฌ๋„ŒํŠธ์—์„œ props๋กœ ์ „๋‹ฌํ•˜๋Š” ๋ฐ์ดํ„ฐ ๋ณ€๊ฒฝ์‹œ ์ „์ฒด ๊ฑฐ๋Œ€ํ•œ ํŠธ๋ฆฌ๊ฐ€ ์—…๋ฐ์ดํŠธ ๋˜๋Š” ๋ฌธ์ œ


์ž์‹์—์„œ ๋ถ€๋ชจ๋กœ ๋ฐ์ดํ„ฐ ์ „๋‹ฌ ๋ฐฉ๋ฒ•

๐Ÿ‘‰ Props๋Š” ์ƒ์œ„ ์ปดํฌ๋„ŒํŠธ์—์„œ ํ•˜์œ„ ์ปดํฌ๋„ŒํŠธ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์ „๋‹ฌํ•˜๋Š” ์šฉ๋„๋กœ๋งŒ ์‚ฌ์šฉํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ž์‹ ์ปดํฌ๋„ŒํŠธ์—์„œ ๋ถ€๋ชจ ์ปดํฌ๋„ŒํŠธ๋กœ props๋ฅผ ํ†ตํ•ด ์ง์ ‘ ๋ฐ์ดํ„ฐ ์ „๋‹ฌ ๋ถˆ๊ฐ€๋Šฅ

๐Ÿ‘‰ ํ•˜์ง€๋งŒ, ์ž์‹ ์ปดํฌ๋„ŒํŠธ๋Š” ๋ถ€๋ชจ ์ปดํฌ๋„ŒํŠธ๋กœ๋ถ€ํ„ฐ ์ „๋‹ฌ๋ฐ›์€ ์ฝœ๋ฐฑ(Callback) ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ ๋ถ€๋ชจ ์ปดํฌ๋„ŒํŠธ์—๊ฒŒ ๋ณ€๊ฒฝ ์‚ฌํ•ญ์„ ์•Œ๋ฆฌ๋Š” ๋ฐฉ์‹์œผ๋กœ๋Š” ์‚ฌ์šฉ ๊ฐ€๋Šฅ

ParentComponent.js

import React, { useState } from "react";

const ParentComponent = () => {
  const [data, setData] = useState(0);
  const handleDataChanged = (data) => {
    setData(data);
  };
  return (
    <div>
      <div>count: {data}</div>
      <ChildComponent handleDataChanged={handleDataChanged} data={data} />;
    </div>
  );
};

const ChildComponent = ({ handleDataChanged, data }) => {
  const handleButtonClick = () => {
    const count = data;
    handleDataChanged(count + 1);
  };
  return <button onClick={handleButtonClick}>์ฆ๊ฐ€</button>;
};

export default ParentComponent;

Result screen
1. ParentComponent์—์„œ data ์ƒํƒœ์™€ handleDataChanged ์ฝœ๋ฐฑ ํ•จ์ˆ˜ ์ •์˜, ์ด ์ฝœ๋ฐฑ ํ•จ์ˆ˜๋ฅผ ChildComponent๋กœ props๋กœ ์ „๋‹ฌ

2. ChildComponent์—์„œ๋Š” ๋ฒ„ํŠผ ํด๋ฆญ ์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ์ธ handleButtonClick ํ•จ์ˆ˜ ์ •์˜, ํด๋ฆญ ์ด๋ฒคํŠธ๊ฐ€ ๋ฐœ์ƒ ์‹œ handleDataChanged ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ data ์ƒํƒœ ์—…๋ฐ์ดํŠธ

3. handleButtonClick ํ•จ์ˆ˜ ๋‚ด์—์„œ๋Š” ํ˜„์žฌ data ๊ฐ’์„ ๊ฐ€์ ธ์™€์„œ 1์„ ๋”ํ•œ ํ›„ ์ด ๊ฐ’์„ handleDataChanged ํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด ๋ถ€๋ชจ ์ปดํฌ๋„ŒํŠธ๋กœ ์ „๋‹ฌ


Props Drilling ํ•ด๊ฒฐ ๋ฐฉ๋ฒ•

Props Drilling์„ ํ”ผํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ์—ฌ๋Ÿฌ๊ฐ€์ง€ ๋ฐฉ๋ฒ•์ด ์žˆ๋Š”๋ฐ

๊ทธ ์ค‘ ๋Œ€ํ‘œ์ ์ธ ๋ฐฉ๋ฒ•์œผ๋กœ๋Š” ์ƒํƒœ๊ด€๋ฆฌ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด๋‹ค.

์ƒํƒœ๊ด€๋ฆฌ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋กœ๋Š” Redux, Recoil, Zustand, Jotai, Mobx ๋“ฑ์ด ์žˆ๋‹ค.

์ƒํƒœ๊ด€๋ฆฌ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์ด์šฉํ•˜๋ฉด ํ•ด๋‹น ๋ฐ์ดํ„ฐ๋ฅผ ํ•„์š”๋กœ ํ•˜๋Š” ์ปดํฌ๋„ŒํŠธ์—์„œ๋งŒ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•˜๊ฒŒ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค.

๋˜ํ•œ, ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ๊ทœ๋ชจ๊ฐ€ ํ™•์žฅ๋˜๋”๋ผ๋„ ์œ ์ง€๋ณด์ˆ˜๊ฐ€ ์šฉ์ดํ•˜๋‹ค.

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