[React] ์ปค์Šคํ…€ ๋ชจ๋‹ฌ ๋งŒ๋“ค๊ธฐ
728x90

Today, What I learned?

ํ”„๋กœ์ ํŠธ์˜ ๊ณต์šฉ ๋ชจ๋‹ฌ์„ ํ™œ์šฉํ•˜๋ ค๊ณ  ํ–ˆ์ง€๋งŒ ๐Ÿ‘€

๊ทธ๋ž˜๋„ ํ™œ์šฉํ•  ์ˆ˜ ์—†๋Š” ํ˜•ํƒœ๋ผ ๋˜ ๋‹ค๋ฅธ ์ปค์Šคํ…€ ๋ชจ๋‹ฌ์„ ๋งŒ๋“ค์–ด ๋ณด๊ธฐ๋กœ ํ–ˆ๋‹ค.

๋”๋ถˆ์–ด ๊ณต์šฉ ๋ชจ๋‹ฌ์„ ์–ด๋–ป๊ฒŒ ๋งŒ๋“ค ์ˆ˜ ์žˆ์„์ง€?? ์ง€๋‚œ ํ”„๋กœ์ ํŠธ์—์„œ ๋งŽ์ด ๊ณ ๋ฏผํ–ˆ์—ˆ๋Š”๋ฐ ์ด๋ฒˆ ํ”„๋กœ์ ํŠธ์—์„œ๋Š” ๋ฉ‹์ง€๊ฒŒ ๊ตฌํ˜„ํ•ด ์ค€ ํŒ€์› ๋ถ„์ด ์žˆ์–ด์„œ!.. ๋งŽ์ด ๋ฐฐ์› ๋‹ค ๐Ÿ˜‡ ๊ทธ๋ž˜์„œ ์˜ค๋Š˜์€ ๋ชจ๋‹ฌ์— ๋Œ€ํ•ด ์ •๋ฆฌํ•ด ๋ณธ๋‹ค.

 

 

useModal hook

๋ชจ๋‹ฌ์„ ๊ณตํ†ต์ ์œผ๋กœ ์“ฐ๋ ค๋ฉด ์ผ๋‹จ ๋ชจ๋‹ฌ์ด ์—ด๋ฆฌ๊ณ  ๋‹ซํžˆ๋Š” ์ƒํƒœ๋ฅผ ๊ด€๋ฆฌํ•ด์ฃผ์–ด์•ผ ํ•œ๋‹ค.

์ด๋ฅผ ์œ„ํ•ด์„œ ๋ณดํ†ต ์ปค์Šคํ…€ ํ›…์œผ๋กœ ๋ชจ๋‹ฌ ์ƒํƒœ๋ฅผ ๊ด€๋ฆฌํ•œ๋‹ค. ์ƒํƒœ๋ฅผ ๋ณ€๊ฒฝํ•ด ์ฃผ๋Š” ํ•จ์ˆ˜ ๊ฐ™์€ ๊ฒƒ์„ ๋งŒ๋“ค์–ด์ฃผ์–ด ๊ด€๋ฆฌํ•œ๋‹ค.
๋˜ ์—ฌ๊ธฐ์— onClick ์ด๋ฒคํŠธ๋‚˜ onChange ์ด๋ฒคํŠธ์ฒ˜๋Ÿผ ํ•„์š”ํ•œ ๊ฒƒ๋“ค์„ ์ถ”๊ฐ€ํ•ด ์ค€๋‹ค.

const useModal = (initialValue: boolean) => {
  const [isOpen, setIsOpen] = useState<boolean>(initialValue);

  const handleModalStateChange = useCallback(() => {
    setIsOpen((prev: boolean) => !prev);
  }, [setIsOpen]);

...

์ „์—ญ์œผ๋กœ ์–ด๋””์—์„œ๋‚˜ ์—ด๋ ค์•ผ ํ•œ๋‹ค๋ฉด ์ด ์ƒํƒœ๋Š” redux๋‚˜.. recoil ๋“ฑ์˜ ์ƒํƒœ๊ด€๋ฆฌ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์ด์šฉํ•ด ์ „์—ญ์œผ๋กœ ๊ด€๋ฆฌํ•ด์ฃผ์–ด์•ผ ํ•œ๋‹ค. ๐Ÿ‘€

๊ณตํ†ต์ ์œผ๋กœ ์‚ฌ์šฉ๋˜๋Š” ๋ชจ๋‹ฌ ์ปดํฌ๋„ŒํŠธ์— ์ƒํƒœ๋“ค๊ณผ ํ•„์š”ํ•œ ํ•จ์ˆ˜๋“ค์„ props๋กœ ์ „๋‹ฌํ•ด ์ฃผ๋Š” ํ˜•ํƒœ๋กœ ์‚ฌ์šฉ!

 

 

์ปค์Šคํ…€ ๋ชจ๋‹ฌ

์ปค์Šคํ…€ ๋ชจ๋‹ฌ๋„ ์šฐ์„  ์—ด๋ฆฌ๊ณ  ๋‹ซํž ์ˆ˜ ์žˆ๋Š” ์ƒํƒœ๊ฐ€ ๊ด€๋ฆฌ๋˜์–ด์•ผ ํ•œ๋‹ค.

๋ชจ๋‹ฌ ์ปดํฌ๋„ŒํŠธ์—๋„ ํ•„์š”ํ•œ props๋“ค์„ ์ „๋‹ฌํ•ด ์ค€๋‹ค.

...
 <ProfileImgEditButton onClick={profileModalStateChange}>
        <EditIcon />
      </ProfileImgEditButton>

      <ProfileImageModal
        currentProfile={profileImg}
        isOpen={isProfileModalOpen}
        onChangeEvent={onChange}
        onDeleteEvent={onDelete}
        handleModalStateChange={profileModalStateChange}
      />

 

๋ชจ๋‹ฌ์ฐฝํ•˜๋ฉด ๋”ค์ฒ˜๋ฆฌ๋œ ๋ ˆ์ด์–ด(๋ฐฑ๋“œ๋กญ)์™€ ์ปจํ…Œ์ด๋„ˆ๊ฐ€ ์žˆ์–ด์•ผ ํ•˜๋‹ˆ.. ๐Ÿ‘€
isOpen ์„ ํ†ตํ•ด์„œ ๋ชจ๋‹ฌ์ฐฝ์ด ๋ณด์ผ ์ˆ˜ ์žˆ๋„๋ก ํ•œ๋‹ค.

 <ModalBackDrop isOpen={isOpen}>
      <ModalContainer isOpen={isOpen}>
        ...
          <ModalButton onClick={handleProfileDeleteClick}>
            ํ”„๋กœํ•„ ์‚ญ์ œ
          </ModalButton>
          <ModalButton onClick={handleProfileEditClick} isConfirm={true}>
            ํ”„๋กœํ•„ ์ˆ˜์ •
          </ModalButton>
        </ModalButtonContainer>
      </ModalContainer>
    </ModalBackDrop>
  );

 

๊ทธ๋Ÿผ ์ด๋ ‡๊ฒŒ ์ž˜ ๋ณด์ธ๋‹ค!..

 

728x90