Framework/React

[React] ๋กœ๋”ฉ๋ฐ”(loading bar) ํ‘œ์‹œ ๋ฐ ๋กœ๋”ฉ ์†๋„ ์กฐ์ ˆํ•˜๋Š” ๋ฐฉ๋ฒ• (+ setTimeout)

yuri lee 2023. 4. 5. 23:40
๋ฐ˜์‘ํ˜•

Intro 

์•ˆ๋…•ํ•˜์„ธ์š”. ์ด๋ฒˆ ์‹œ๊ฐ„์—๋Š” ๋ฆฌ์•กํŠธ์—์„œ ๋กœ๋”ฉ๋ฐ” ํ‘œ์‹œ ๋ฐ ๋กœ๋”ฉ ์†๋„๋ฅผ ์กฐ์ ˆํ•˜๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. 

 

How to solve the problem

๋‹ค์Œ์˜ List ์ปดํฌ๋„ŒํŠธ๊ฐ€ ์žˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•ด๋ด…์‹œ๋‹ค. handleFilterClickํ•จ์ˆ˜๋ฅผ ํด๋ฆญ ํ• ๋•Œ๋งˆ๋‹ค ํ•„ํ„ฐ๊ธฐ๋Šฅ์ด ์ž‘๋™๋˜๋Š” ๋กœ์ง์ž…๋‹ˆ๋‹ค. ํ•„ํ„ฐ๋ฅผ ๋ˆ„๋ฅผ ๋•Œ๋งˆ๋‹ค ํ•„ํ„ฐ๊ฐ€ ๋œ๋“ฏํ•œ ๋™์ž‘์„ ํ‘œํ˜„ํ•˜๊ธฐ ์œ„ํ•ด ๋กœ๋”ฉ๋ฐ”๋ฅผ ์ถ”๊ฐ€๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. 

const List: NextPage<Props> = () => {

  const {dataList, setDataList} = useLoginStore();
  const [totalCtn, setTotalCtn] = useState<number>();
  const [filterStatus, setFilterStatus] = useState("ALL");

  const handleFilterClick = (status: string) => {
    setFilterStatus(status);
  };

  const isFiltered = () => {
    // some conditional
  };

  return (
    <div>
      <div onClick={() => handleFilterClick("ALL")}>{data && totalCtn}</div>
      <div onClick={() => handleFilterClick("TOP")}>{data.top}</div>
      <div onClick={() => handleFilterClick("SKIRT")}>{data.skirt}</div>
      {dataList?.map((item, index: number) => {
        if (isFiltered()) {
          return (
            <div>
              <button type="button">
                <strong>{item.name}</strong>
              </button>
            </div>
          );
        }
      })}
    </div>
  );
};

export default List;

 

๋กœ๋”ฉ๋ฐ” ์ถ”๊ฐ€ํ•˜๊ธฐ

์šฐ์„  ๋กœ๋”ฉ๋ฐ” ํ‘œ์‹œ๋ฅผ ์œ„ํ•ด ์ƒํƒœ๊ฐ’์„ ์•„๋ž˜์™€ ๊ฐ™์ด ์ •์˜ํ•ด์ค๋‹ˆ๋‹ค. 

const [isLoading, setIsLoading] = useState(false);

 

์ด๋ฏธ ์กด์žฌํ•˜๋Š” ๋ฐ์ดํ„ฐ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋ฟŒ๋ ค์ฃผ๊ธฐ ๋•Œ๋ฌธ์— ์ˆœ์‹๊ฐ„์— ํ•„ํ„ฐ๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ๋กœ๋”ฉ๋ฐ” ํ‘œ์‹œ๋ฅผ ์œ„ํ•ด์„œ๋Š” ํ•„ํ„ฐ ํดํ•˜๋Š” ์ˆœ๊ฐ„ ์‹œ๊ฐ„์„ ์ž ๊น์ด๋‚˜๋งˆ ์ง€์—ฐ์‹œ์ผœ์•ผ ํ–ˆ์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ์ €๋Š” ์•„๋ž˜์™€ ๊ฐ™์ด setTimeout()์„ ํ™œ์šฉํ•ด์„œ ์งง์€ ํ…€์„ ์ถ”๊ฐ€ํ•ด ์คฌ์Šต๋‹ˆ๋‹ค.

 

์ฆ‰ handleFilterClick ํ•จ์ˆ˜๊ฐ€ ํด๋ฆญ๋˜๋Š” ์ˆœ๊ฐ„ isLoading ๊ฐ’์„ true๋กœ ๋งŒ๋“ค๊ณ , ์‹œ๊ฐ„ํ…€์„ ๋‘” ํ›„์— isLoading๊ฐ’์„ false๋กœ ๋งŒ๋“ค์—ˆ๋‹ค๊ณ  ๋ณด๋ฉด ๋ฉ๋‹ˆ๋‹ค. 

const handleFilterClick = (status: string) => {
    setIsLoading(true);
    setTimeout(() => {
      setIsLoading(false);
      setFilterStatus(status);
    }, 100);
};

 

๋ Œ๋”๋ง๋œ ์ปดํฌ๋„ŒํŠธ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ฒ˜๋ฆฌํ•ด์คฌ์Šต๋‹ˆ๋‹ค. isLoading๊ฐ’์ด true์ผ ๋•Œ Loding ์ปดํฌ๋„ŒํŠธ๋ฅผ ํ‘œ์‹œํ•ด์คฌ์Šต๋‹ˆ๋‹ค. 

<div>
  <div onClick={() => handleFilterClick("ALL")}>{data && totalCtn}</div>
  <div onClick={() => handleFilterClick("TOP")}>{data.top}</div>
  <div onClick={() => handleFilterClick("SKIRT")}>{data.skirt}</div>
  {isLoading ? (
    <Loding />
  ) : (
    <>
      {dataList?.map((item, index: number) => {
        if (data && isFiltered()) {
          return (
            <div>
              <button type="button">
                <strong>{item.name}</strong>
              </button>
            </div>
          );
        }
      })}
    </>
  )}
</div>

 


https://developer.mozilla.org/en-US/docs/Web/API/setTimeout

๋ฐ˜์‘ํ˜•