๐Ÿ”ฅํŽ˜์ด์ง€ ๋‚ด state์™€ prop์ด ๋„ˆ๋ฌด ๋งŽ๋‹ค๋ฉด?

๋ฐ•๋ฏผ์šฐยท2023๋…„ 12์›” 12์ผ
1

๐Ÿ”ฅ์˜ฌํ•ด๋„ ์•„์ขŒ์ขŒ

๋ชฉ๋ก ๋ณด๊ธฐ
1/3

์˜ฌํ•ด๋„ ์•„์ขŒ์ขŒ ํ”„๋กœ์ ํŠธ๋ฅผ ์ง„ํ–‰ํ•˜๋ฉฐ ํ•˜๋‚˜์˜ ํŽ˜์ด์ง€ ๋‚ด์—์„œ ๊ด€๋ฆฌ๋˜๋Š” state์™€ ํ•˜์œ„ ์ปดํฌ๋„ŒํŠธ๋กœ ์ „๋‹ฌ๋˜๋Š” prop์ด ๋„ˆ๋ฌด ๋งŽ์•„์ง€๋Š” ๋ฌธ์ œ๋ฅผ ๊ฒฝํ—˜ํ–ˆ๊ณ , ์ด๋ฅผ ํ•ด๊ฒฐํ•˜๋Š” ๊ณผ์ •์„ ๋‹ด์€ ๊ธ€์ž…๋‹ˆ๋‹ค.


๐Ÿ“Œ ํ•˜๋‚˜์˜ ํŽ˜์ด์ง€ ๋‚ด ์—ฌ๋Ÿฌ ๊ฐœ์˜ state

์ €๋Š” ์˜ฌํ•ด๋„ ์•„์ขŒ์ขŒ ํ”„๋กœ์ ํŠธ์—์„œ ๊ณ„ํš ์ž‘์„ฑ ํŽ˜์ด์ง€์˜ ๊ตฌํ˜„์„ ๋‹ด๋‹นํ–ˆ์Šต๋‹ˆ๋‹ค.

๋‹ค์Œ๊ณผ ๊ฐ™์€ ํŽ˜์ด์ง€์ธ๋ฐ, ํ•œ ๋ˆˆ์— ๋ณด๊ธฐ์—๋„ ํŽ˜์ด์ง€ ๋‚ด์—์„œ ๊ด€๋ฆฌ๋˜๋Š” data๋“ค์ด ๋งŽ์•„ ๋ณด์ž…๋‹ˆ๋‹ค๐Ÿ˜…

๊ณ„ํš ์ž‘์„ฑ ํŽ˜์ด์ง€์˜ ์—ญํ• ์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

  • ์‚ฌ์šฉ์ž๋กœ๋ถ€ํ„ฐ ๊ณ„ํš ์ž‘์„ฑ์— ํ•„์š”ํ•œ data๋ฅผ ์ž…๋ ฅ ๋ฐ›์Šต๋‹ˆ๋‹ค.
  • ๊ณ„ํš ์ƒ์„ฑ ๋ฒ„ํŠผ์„ ๋ˆŒ๋ €์„ ๋•Œ ๊ณ„ํš ์ž‘์„ฑ API๋ฅผ ํ˜ธ์ถœํ•ด ๊ณ„ํš์„ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.

ํŽ˜์ด์ง€ level์—์„œ ๊ณ„ํš ์ž‘์„ฑ API๋ฅผ ํ˜ธ์ถœํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ๊ฒฐ๊ตญ ๋ชจ๋“  data๋“ค์ด ํ•œ ๋ฒˆ์— request body์— ๋‹ด๊ฒจ์ ธ ์„œ๋ฒ„์— ์ „์†ก์ด ๋˜์–ด์•ผ ํ•˜๋ฏ€๋กœ, ๊ณ„ํš ์ž‘์„ฑ์— ํ•„์š”ํ•œ ๋ชจ๋“  data๋Š” ํŽ˜์ด์ง€ level์—์„œ ์ •์˜ํ•˜๊ณ , ํ•˜์œ„ ์ปดํฌ๋„ŒํŠธ์— prop์œผ๋กœ ๋„˜๊ฒจ์ฃผ๋Š” ๋ฐฉ์‹์œผ๋กœ ๊ตฌํ˜„ํ•˜๊ธฐ๋กœ ๊ฒฐ์ •ํ–ˆ์Šต๋‹ˆ๋‹ค.

์ด๋ ‡๊ฒŒ ๊ฒฐ์ •ํ•œ ํ›„, ํŽ˜์ด์ง€ ๋‚ด ๊ฐ ์ปดํฌ๋„ŒํŠธ๋ฅผ ๋‚˜๋ˆ ๋ณด๋‹ˆ ํŽ˜์ด์ง€ ๋‚ด ๊ฐ€์žฅ ํ•˜์œ„ ์ปดํฌ๋„ŒํŠธ์— ํ•ด๋‹นํ•˜๋Š” RemindInput์— state๊ฐ€ ์ „๋‹ฌ๋˜๊ธฐ๊นŒ์ง€ ์ด 4๊ฐœ์˜ ์ปดํฌ๋„ŒํŠธ๋ฅผ ๊ฑฐ์ณ์•ผ ํ•˜๋Š” prop drilling ํ˜„์ƒ์ด ๋ฐœ์ƒํ–ˆ์Šต๋‹ˆ๋‹ค.

์•„๋ž˜ ๊ทธ๋ฆผ์—์„œ ํŒŒ๋ž€์ƒ‰ ์ง์‚ฌ๊ฐํ˜•์œผ๋กœ ํ‘œ์‹œ๋œ ์ปดํฌ๋„ŒํŠธ๊ฐ€ ์ตœํ•˜์œ„ ์ปดํฌ๋„ŒํŠธ์ธ RemindInput์ž…๋‹ˆ๋‹ค.

์‚ฌ์‹ค prop์˜ ์ „๋‹ฌ์ด 4๊ฐœ์˜ ์ปดํฌ๋„ŒํŠธ๋ฅผ ๊ฑฐ์น˜๋Š” ๊ฒƒ์ด depth๊ฐ€ ์—„์ฒญ ๊นŠ์€ ํŽธ์€ ์•„๋‹ˆ๋ผ๊ณ  ์ƒ๊ฐํ–ˆ์ง€๋งŒ, ํ•˜์œ„๋กœ ์ „๋‹ฌ๋˜์–ด์•ผ ํ•˜๋Š” ๋ชจ๋“  data์™€ ๋ฉ”์„œ๋“œ๋“ค์ด CreatePage์—์„œ ๊ด€๋ฆฌ๋˜์—ˆ๊ธฐ ๋•Œ๋ฌธ์— ๊ทธ ์ˆ˜๊ฐ€ ๋„ˆ๋ฌด ๋งŽ์•˜์Šต๋‹ˆ๋‹ค.

export default function CreatePage() {
  const router 
  const [title, setTitle] = useState
  const [description, setDescription] = useState('');
  const [tags, setTags] = useState<string[]>([]);
  const [isPublic, setPublic] = useState(true);
  const toggleIsPublic = () => {
    setPublic(!isPublic);
  };
    
  const [remindOptions, setRemindOptions] = useState<RemindOptionType>({
    TotalPeriod: 12,
    Term: 1,
    Date: 1,
    Time: 9,
  });
    
  const [remindMessageList, setRemindMessageList] = useState<RemindItemType[]>(
    [],
  );
    
  const handleChangeRemindOption = () => {};
  const handleChangeRemindMessage = () => {};
  const fixRemindOptions = () => {};
  const makeAllRemindMessageSame = () => {};	

์ž‘์„ฑ ํŽ˜์ด์ง€์—์„œ ๊ด€๋ฆฌ๋˜์–ด์•ผ ํ•˜๋Š” state์™€ ์ด state๋ฅผ ๋‹ค๋ฃจ๋Š” ๋ฉ”์„œ๋“œ๋“ค์€ ๋Œ€๋žต ์œ„์™€ ๊ฐ™์•˜์Šต๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ๋งŽ์€ state์— ๋Œ€ํ•œ prop drilling์„ ํ†ตํ•ด ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋ฌธ์ œ๋“ค์ด ๋ฐœ์ƒํ–ˆ์Šต๋‹ˆ๋‹ค.

  • ๊ธธ์–ด์ง€๋Š” ์ฝ”๋“œ๋กœ ์ธํ•ด ์ฝ”๋“œ์˜ ๊ฐ€๋…์„ฑ์ด ๋งค์šฐ ๋‚˜๋น ์ง‘๋‹ˆ๋‹ค.
  • ์ค‘๊ฐ„ ์ปดํฌ๋„ŒํŠธ์— ๋ถˆํ•„์š”ํ•œ ํ”„๋กœํผํ‹ฐ๊ฐ€ ์ „๋‹ฌ๋˜์–ด ๋ถˆํ•„์š”ํ•œ ๋ณต์žก์„ฑ์„ ์ดˆ๋ž˜ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  • state ๋ณ€๊ฒฝ ์‹œ props ์ „๋‹ฌ ๊ณผ์ •์— ๊ด€์—ฌ๋œ ์ปดํฌ๋„ŒํŠธ๋“ค์—๊ฒŒ ๋ถˆํ•„์š”ํ•œ ๋ฆฌ๋ Œ๋”๋ง์ด ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๊ทธ๋ž˜์„œ ์œ„์™€ ๊ฐ™์€ ๋ฌธ์ œ์ ๋“ค์„ ์ธ์ง€ํ•œ ํ›„, ์ข€ ๋” ํšจ์œจ์ ์ธ ๋ฐฉ๋ฒ•์œผ๋กœ ํŽ˜์ด์ง€ ๋‚ด์˜ data๋“ค์„ ๊ด€๋ฆฌํ•  ์ˆ˜ ์—†์„๊นŒ ๊ณ ๋ฏผ์„ ์‹œ์ž‘ํ–ˆ์Šต๋‹ˆ๋‹ค.


๐Ÿ“Œ Recoil? ContextAPI?

prop์œผ๋กœ ์ƒํƒœ๋ฅผ ํ•˜์œ„์— ์ „๋‹ฌํ•˜๋Š” ๋ฐฉ๋ฒ•์˜ ๋ฌธ์ œ์ ๋“ค์„ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด, Recoil๊ณผ ContextAPI์˜ ์‚ฌ์šฉ์„ ๊ณ ๋ คํ•˜๊ฒŒ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.

์ƒํƒœ ๊ด€๋ฆฌ ๋„๊ตฌ์ธ Recoil๊ณผ ContextAPI ๋ชจ๋‘, state๋ฅผ ํ•˜์œ„ ์ปดํฌ๋„ŒํŠธ์— prop์œผ๋กœ ์ „๋‹ฌํ•˜๋Š” ๋ฐฉ์‹์ด ์•„๋‹ˆ๋ผ ์›ํ•˜๋Š” ์œ„์น˜์—์„œ data๋ฅผ ๋ถˆ๋Ÿฌ์™€ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์œ„์—์„œ ์–ธ๊ธ‰ํ•œ ์ฝ”๋“œ์˜ ๊ฐ€๋…์„ฑ ๋ฌธ์ œ์™€ ๋ถˆํ•„์š”ํ•œ ํ”„๋กœํผํ‹ฐ ์ „๋‹ฌ ๋ฌธ์ œ ๋ชจ๋‘ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์ฒ˜๋Ÿผ ๋ณด์˜€์Šต๋‹ˆ๋‹ค.

๋˜ํ•œ, Recoil์„ ์‚ฌ์šฉํ–ˆ์„ ๋•Œ์—๋„ ์—ฌ์ „ํžˆ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ๋Š” ๋ถˆํ•„์š”ํ•œ ๋ Œ๋”๋ง ๋ฌธ์ œ๋ฅผ ๋ง‰๊ธฐ ์œ„ํ•ด atom ๊ฐ์ฒด์— ๊ฐ ์†์„ฑ์— ๋Œ€ํ•œ selector์˜ get๊ณผ set์„ ์ •์˜ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์ƒ๊ฐํ•ด ํŒ€์›๋ถ„์—๊ฒŒ ์ด๋ฅผ ์ œ์•ˆํ•˜๊ธฐ๋„ ํ–ˆ์Šต๋‹ˆ๋‹ค.

ํ•˜์ง€๋งŒ, prop์œผ๋กœ data๋ฅผ ์ „๋‹ฌํ•˜๋Š” ๋ฐฉ์‹์˜ ๋ฌธ์ œ์ ์„ ํ•ด๊ฒฐํ•ด์ฃผ๋Š” Recoil๊ณผ ContextAPI๋ฅผ ์‚ฌ์šฉํ–ˆ์„ ๋•Œ์—๋„, ์ƒˆ๋กœ์šด ๋ฌธ์ œ๊ฐ€ ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค.

๐Ÿ’ก ์ปดํฌ๋„ŒํŠธ์—์„œ ์ „์—ญ ์ƒํƒœ๋ฅผ ๊ตฌ๋…ํ•˜๋Š” ์ˆœ๊ฐ„, ๊ทธ ์ปดํฌ๋„ŒํŠธ๋Š” ์žฌ์‚ฌ์šฉ์ด ์–ด๋ ค์›Œ์ง„๋‹ค.

์œ„์˜ ๊ทธ๋ฆผ์—์„œ ๋ณด๋“ฏ์ด ์™ผ์ชฝ์˜ ๊ณ„ํš ์ž‘์„ฑ ํŽ˜์ด์ง€์™€ ์˜ค๋ฅธ์ชฝ์˜ ๊ณ„ํš ์ˆ˜์ • ํŽ˜์ด์ง€๋Š” ํ•œ ๋ˆˆ์— ๋ณด๊ธฐ์—๋„ ๊ทธ ๊ตฌ์กฐ๊ฐ€ ์ƒ๋‹นํžˆ ์œ ์‚ฌํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ ์ด ๋‘๊ฐœ์˜ ํŽ˜์ด์ง€์—์„œ๋Š” ๊ณตํ†ต์œผ๋กœ ์‚ฌ์šฉ๋˜๋Š” WritableRemind ์ปดํฌ๋„ŒํŠธ์™€ WritableRemindItem ์ปดํฌ๋„ŒํŠธ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.

ํ•˜์ง€๋งŒ ์ž‘์„ฑ ํŽ˜์ด์ง€์™€ ์ˆ˜์ • ํŽ˜์ด์ง€๋Š” ์„œ๋กœ ์—ญํ• ์ด ๋‹ค๋ฅธ ํŽ˜์ด์ง€์ด๊ณ , ํ˜ธ์ถœ๋˜์–ด์•ผ ํ•˜๋Š” API๊ฐ€ ๋‹ค๋ฆ…๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ, ํ•„์š”ํ•œ data์˜ ๊ตฌ์กฐ์™€ ์„ฑ๊ฒฉ์ด ์กฐ๊ธˆ์”ฉ ๋‹ค๋ฆ…๋‹ˆ๋‹ค. ๊ทธ๋ ‡๊ธฐ ๋•Œ๋ฌธ์— ๋งŒ์•ฝ Recoil์„ ์‚ฌ์šฉํ•œ๋‹ค๋ฉด, ๊ฐ๊ฐ ์„œ๋กœ ๋‹ค๋ฅธ atom์„ ๊ตฌ๋…ํ•ด์•ผํ•˜๊ณ  ์ด๋ ‡๊ฒŒ ๋˜๋ฉด ๋‘ ํŽ˜์ด์ง€์—์„œ ๊ณตํ†ต์œผ๋กœ ์“ฐ์ด๋Š” ์ปดํฌ๋„ŒํŠธ๋Š” ๊ฐ ํŽ˜์ด์ง€์—์„œ ์‚ฌ์šฉ๋˜๋Š” ์„œ๋กœ ๋‹ค๋ฅธ ๋‘ ๊ฐœ์˜ atom์„ ๊ตฌ๋…ํ•˜๊ณ  ์žˆ์–ด์•ผ ํ•˜๋‚˜,,? ๋ผ๋Š” ์ƒ๊ฐ์ด ๋“ค์—ˆ์Šต๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ๋˜๋ฉด ๊ทธ ์ปดํฌ๋„ŒํŠธ๊ฐ€ ๋‹ค๋ฃจ์–ด์•ผ ํ•˜๋Š” data์˜ ์ˆ˜๋„ ๋งŽ์•„์งˆ ๊ฒƒ์ด๊ณ , ์ž์นซํ•˜๋ฉด ์ปดํฌ๋„ŒํŠธ๋ฅผ ๋ถ„๋ฆฌํ•ด์•ผ ํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค.

prop์œผ๋กœ data๋ฅผ ์ „๋‹ฌํ•˜๋Š” ๋ฐฉ์‹์˜ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด ์ „์—ญ ์ƒํƒœ ๊ด€๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•˜๋ ค๊ณ  ํ–ˆ์ง€๋งŒ, ํŠน์ • ์ปดํฌ๋„ŒํŠธ๊ฐ€ ์ „์—ญ ์ƒํƒœ๋ฅผ ๊ตฌ๋…ํ•˜๋Š” ์ˆœ๊ฐ„ ๊ทธ ์ปดํฌ๋„ŒํŠธ์˜ ์žฌ์‚ฌ์šฉ์„ฑ์ด ์ค„์–ด๋“œ๋Š” ๋ฌธ์ œ๊ฐ€ ์˜ˆ์ƒ๋˜์—ˆ๊ณ , ์ด๋Š” ์ ์ ˆํ•œ ํ•ด๊ฒฐ๋ฐฉ๋ฒ•์ด ์•„๋‹Œ ๊ฒƒ ๊ฐ™์•„ ๋„์ž…ํ•˜์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ค.


๐Ÿ“Œ prop ๋ฐฉ์‹ ์‚ฌ์šฉ, but ๋ฆฌ์•กํŠธ ํ›… ์ตœ์ ํ™”ํ•˜๊ธฐ!

๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•œ ๋‹ค๋ฅธ ๋ฐฉ๋ฒ•๋“ค๋„ ์ƒ๊ฐํ•ด๋ณด์•˜์Šต๋‹ˆ๋‹ค.

  • React-hook-form ์‚ฌ์šฉ
  • ํŽ˜์ด์ง€์˜ ๋ถ„๋ฆฌ => ๊ณ„ํš ์ž‘์„ฑ์„ ์—ฌ๋Ÿฌ ๋‹จ๊ณ„๋กœ

๊ตฌํ˜„ ์ค‘์ด์—ˆ๋˜ ๊ณ„ํš ์ž‘์„ฑ ํŽ˜์ด์ง€๋Š” ์‚ฌ์‹ค์ƒ ์‚ฌ์šฉ์ž๋กœ๋ถ€ํ„ฐ data๋ฅผ ์ž…๋ ฅ๋ฐ›์•„, ์ด๋ฅผ ์กฐํ•ฉํ•ด ์„œ๋ฒ„๋กœ ์ „์†กํ•˜๋Š” ํผ์˜ ์—ญํ• ์„ ํ–ˆ๊ธฐ ๋•Œ๋ฌธ์— React-hook-form์ด๋ผ๋Š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์ข€ ๋” ์‰ฝ๊ฒŒ ์ƒํƒœ ๊ด€๋ฆฌ๋ฅผ ํ•  ์ˆ˜ ์žˆ์ง€ ์•Š์„๊นŒ๋ผ๋Š” ์ƒ๊ฐ๋„ ๋“ค์—ˆ์Šต๋‹ˆ๋‹ค.

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

ํ•˜์ง€๋งŒ ๊ฒฐ๋ก ์ ์œผ๋กœ๋Š”, ๋‘ ๋ฐฉ์‹ ๋ชจ๋‘ ๋„์ž…ํ•˜์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ค.

๊ทธ ์ด์œ ๋Š”, ๋‘ ๊ฐ€์ง€ ๋ฐฉ๋ฒ• ๋ชจ๋‘ "์‹œ๊ฐ„" ์ด๋ผ๋Š” ์ถ”๊ฐ€์  ๋น„์šฉ์ด ์ƒ๋Œ€์ ์œผ๋กœ ๋งŽ์ด ๋ฐœ์ƒํ•˜๋Š” ๋ฐฉ๋ฒ•์ด์—ˆ๊ณ  ํ”„๋กœ์ ํŠธ ์™„์„ฑ์ด๋ผ๋Š” ๋ชฉํ‘œ๋ฅผ ์ƒ๊ฐํ–ˆ์„ ๋•Œ, ์ข‹์€ ํ•ด๊ฒฐ ๋ฐฉ๋ฒ•์ด ์•„๋‹ˆ์—ˆ๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค.

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

๊ทธ๋ž˜์„œ ๊ฒฐ๊ตญ ์ €๋Š” ๊ธฐ์กด์ฒ˜๋Ÿผ prop์œผ๋กœ data๋ฅผ ํ•˜์œ„๋กœ ์ „๋‹ฌํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ state๋ฅผ ๊ด€๋ฆฌํ•˜๋˜, ๋ถˆํ•„์š”ํ•œ ๋ Œ๋”๋ง์ด ๋ฐœ์ƒํ•˜์ง€ ์•Š๋„๋ก ๋ฆฌ์•กํŠธ ํ›… ์ตœ์ ํ™”๋ฅผ ์ง„ํ–‰ํ•˜๊ณ  ํ”„๋กœ์ ํŠธ ์™„์„ฑ ์ดํ›„ ํŽ˜์ด์ง€๋ฅผ ๋ถ„๋ฆฌํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ์ง„ํ–‰ํ•˜๊ธฐ๋กœ ๊ฒฐ์ •ํ–ˆ์Šต๋‹ˆ๋‹ค.


๋ฆฌ์•กํŠธ ํ›…์„ ์ตœ์ ํ™”ํ–ˆ๋˜ ์˜ˆ์‹œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

useMemo()

const filteredTermOptions = useMemo(() => {
  return TERM_OPTIONS.filter(
    (option) => option.value <= remindOptions.TotalPeriod,
  );
}, [remindOptions.TotalPeriod]);
const isRemindMessageEmpty = useMemo(() => {
  return remindMessage.length === 0;
}, [remindMessage]);

๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋ Œ๋”๋ง ์‹œ๋งˆ๋‹ค ์žฌ์„ ์–ธ ๋  ํ•„์š”๊ฐ€ ์—†๋Š” ๋ณ€์ˆ˜๋Š” ํŠน์ • data๊ฐ€ ๋ณ€ํ•  ๋•Œ๋งŒ ์žฌ์„ ์–ธ ๋  ์ˆ˜ ์žˆ๋„๋ก ํ•ด์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค.

React.Memo()

export default React.memo(function WritableRemindItem({
  remindMonth,
  remindDay,
  remindMessage,
  handleChangeRemindMessage,
  makeAllRemindMessageSame,
  classNameList = [],
}: WritableRemindItemProps) {
// ์ดํ•˜ ์ƒ๋žต

์ž‘์„ฑ ํŽ˜์ด์ง€์—์„œ๋Š” prop์œผ๋กœ data๊ฐ€ ์ „๋‹ฌ๋˜๋Š” depth๊ฐ€ ์ตœ๋Œ€ 4๋ฒˆ๊นŒ์ง€ ์žˆ์—ˆ๊ธฐ์—, ์ž์‹ ์ปดํฌ๋„ŒํŠธ๊ฐ€ ๋ถˆํ•„์š”ํ•˜๊ฒŒ ๋ฆฌ๋ Œ๋”๋ง ๋  ์ˆ˜ ์žˆ๋Š” ์ƒํ™ฉ์ด ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค.

๊ทธ๋ž˜์„œ ์ž‘์„ฑ ํŽ˜์ด์ง€ ์ปดํฌ๋„ŒํŠธ๋‚˜ ์ƒ์œ„ ์ปดํฌ๋„ŒํŠธ์˜ ์ƒํƒœ๊ฐ€ ๋ณ€๊ฒฝ๋˜์–ด ๋ฆฌ๋ Œ๋”๋ง ๋  ๋•Œ, ํ•˜์œ„ ์ปดํฌ๋„ŒํŠธ์˜ ์ƒํƒœ๋‚˜ prop์ด ๋ณ€๊ฒฝ๋˜์ง€ ์•Š์•˜๋‹ค๋ฉด ํ•˜์œ„ ์ปดํฌ๋„ŒํŠธ๋Š” ๋ถˆํ•„์š”ํ•œ ๋ฆฌ๋ Œ๋”๋ง์ด ์ผ์–ด๋‚˜์ง€ ์•Š๋„๋ก React.memo()๋ฅผ ์‚ฌ์šฉํ•ด์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค.

useCallback()

const handleChangeRemindOption = useCallback(
  (optionKey: string, newOptionValue: number) => {
    setRemindOptions
      ...remindOptions,
      [optionKey]: newOptionValue,
    });
  },
  [remindOptions, setRemindOptions],
);

๋ฆฌ์•กํŠธ์—์„œ ์ปดํฌ๋„ŒํŠธ๊ฐ€ ๋ฆฌ๋ Œ๋”๋ง ๋  ๋•Œ, ์ปดํฌ๋„ŒํŠธ ๋‚ด ์ •์˜๋œ ํ•จ์ˆ˜๋„ ์žฌ์ •์˜๋˜๊ธฐ ๋•Œ๋ฌธ์— ๋งŒ์•ฝ ์ด ํ•จ์ˆ˜๊ฐ€ ์ž์‹ ์ปดํฌ๋„ŒํŠธ์—๊ฒŒ ์ „๋‹ฌ๋œ๋‹ค๋ฉด ์ž์‹ ์ปดํฌ๋„ŒํŠธ๋กœ ์ „๋‹ฌ๋˜๋Š” prop์ด ๋ณ€ํ•˜๋Š” ๊ฒƒ์ด๊ธฐ ๋•Œ๋ฌธ์— ์ž์‹ ์ปดํฌ๋„ŒํŠธ๋„ ๋ฆฌ๋ Œ๋”๋ง ๋  ๊ฒƒ์ž…๋‹ˆ๋‹ค.

์œ„์˜ handleChangeRemindOption ๋ฉ”์„œ๋“œ๋Š” ํ•˜์œ„ ์ปดํฌ๋„ŒํŠธ๋กœ ์ „๋‹ฌ๋˜๋Š” ํ•จ์ˆ˜์˜€๊ธฐ์—, ๋ Œ๋”๋ง ๋  ๋•Œ๋งˆ๋‹ค ํ•˜์œ„ ์ปดํฌ๋„ŒํŠธ๊ฐ€ ๊ณ„์† ๋ฆฌ๋ Œ๋”๋ง ๋  ๊ฒƒ์ž…๋‹ˆ๋‹ค.

๊ทธ๋ž˜์„œ useCallback์„ ์‚ฌ์šฉํ•ด ์—ฐ๊ด€๋œ data๊ฐ€ ๋ณ€ํ•˜์ง€ ์•Š๋Š”๋‹ค๋ฉด ํ•จ์ˆ˜๋ฅผ ์žฌ์ •์˜ํ•˜์ง€ ์•Š๋„๋ก ํ•ด์ฃผ์–ด ํ•˜์œ„ ์ปดํฌ๋„ŒํŠธ์˜ ๋ถˆํ•„์š”ํ•œ ๋ฆฌ๋ Œ๋”๋ง์„ ๋ฐฉ์ง€ํ–ˆ์Šต๋‹ˆ๋‹ค.


๐Ÿ“Œ ๊ฒฐ๋ก 

์œ„์˜ ๊ธ€์„ ์ •๋ฆฌํ•ด๋ณด์ž๋ฉด, ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

  1. ํŽ˜์ด์ง€ ๋‚ด์—์„œ ๊ด€๋ฆฌํ•ด์•ผ ํ•  state์™€ prop์ด ๋„ˆ๋ฌด ๋งŽ๋‹ค๋Š” ๋ฌธ์ œ๋ฅผ ๋งˆ์ฃผํ–ˆ์Šต๋‹ˆ๋‹ค.
  2. ์ด๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด ์—ฌ๋Ÿฌ๊ฐ€์ง€ ๋ฐฉ๋ฒ•์„ ๊ณ ๋ คํ•ด๋ณด์•˜์Šต๋‹ˆ๋‹ค.
    • Recoil
    • ContextAPI
    • React-hook-form
    • ํŽ˜์ด์ง€์˜ ๋ถ„๋ฆฌ
  3. ํŽ˜์ด์ง€์—์„œ state์™€ prop์œผ๋กœ data๋ฅผ ๊ด€๋ฆฌํ•˜๋˜, ๋ฆฌ์•กํŠธ ํ›…์„ ์ตœ์ ํ™”ํ•˜๋Š” ๋ฐฉ๋ฒ•์œผ๋กœ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ–ˆ์Šต๋‹ˆ๋‹ค.

์•ž์œผ๋กœ๋„ ๊ฐœ๋ฐœ์„ ํ•˜๋ฉฐ ํฌ๊ณ  ์ž‘์€ ๋‹ค์–‘ํ•œ ๋ฌธ์ œ๋“ค์„ ๋งŒ๋‚ ํ…๋ฐ, ๊ทธ ๋•Œ๋งˆ๋‹ค ์ด๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•œ ์—ฌ๋Ÿฌ ๊ฐ€์ง€ ๋ฐฉ๋ฒ•๋“ค์˜ ์žฅ์ ๊ณผ ๋น„์šฉ์„ ๊ณ„์‚ฐํ•ด๋ณด๊ณ , ์ด๋ฅผ ๋ฐ”ํƒ•์œผ๋กœ ๊ทผ๊ฑฐ ์žˆ๊ณ  ํ•ฉ๋ฆฌ์ ์ธ ์„ ํƒ์„ ํ•˜๋Š” ๊ฐœ๋ฐœ์ž๊ฐ€ ๋˜๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค!

profile
๊พธ์ค€ํžˆ, ๊นŠ๊ฒŒ

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