[React] Material UI ์ด์šฉํ•ด์„œ ํŽ˜์ด์ง€๋„ค์ด์…˜ ์ ์šฉํ•˜๊ธฐ
728x90

ํŽ˜์ด์ง€๋„ค์ด์…˜? ๋ฌดํ•œ ์Šคํฌ๋กค?

ํ”„๋กœ์ ํŠธ๋ฅผ ์ง„ํ–‰ํ•˜๋ฉฐ ๊ฒŒ์‹œ๊ธ€ ์ฝ˜ํ…์ธ  ์ˆ˜๊ฐ€ ๋„ˆ๋ฌด ๋งŽ์•„์งˆ ๊ฒฝ์šฐ๋ฅผ ๋Œ€๋น„ํ•ด์„œ ํŽ˜์ด์ง€๋„ค์ด์…˜์ด๋‚˜ ๋ฌดํ•œ์Šคํฌ๋กค์„ ์ ์šฉํ•ด๋ณด๊ธฐ๋กœ ํ–ˆ๋‹ค.

๊ฒŒ์‹œ๊ธ€ ํŠน์„ฑ์ƒ, ํ•˜๋‹จ์— ๊ธ€์“ฐ๊ธฐ ๋ฒ„ํŠผ์ด ํ•ญ์ƒ ์žˆ์–ด์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋ฌดํ•œ์Šคํฌ๋กค๋ณด๋‹ค๋Š” ํŽ˜์ด์ง€๋„ค์ด์…˜์ด ๋” ์ ํ•ฉํ•˜๊ฒ ๋‹ค๋Š” ์ƒ๊ฐ์œผ๋กœ!..

ํŽ˜์ด์ง€๋„ค์ด์…˜์„ ๊ตฌํ˜„ํ•ด๋ณด๊ธฐ๋กœ ํ–ˆ๋‹ค.

 

 

MUI์˜ Pagination ์ปดํฌ๋„ŒํŠธ

MUI์—์„œ ํŽ˜์ด์ง€๋„ค์ด์…˜ ์ปดํฌ๋„ŒํŠธ๋ฅผ ๊ฒ€์ƒ‰ํ•˜๋ฉด ๋‹ค์–‘ํ•œ ์Šคํƒ€์ผ์˜ ํŽ˜์ด์ง€๋„ค์ด์…˜ ์ปดํฌ๋„ŒํŠธ๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

https://mui.com/material-ui/react-pagination/#main-content

 

React Pagination component - Material UI

The Pagination component enables the user to select a specific page from a range of pages.

mui.com

 

ํ˜„์žฌ ํ”„๋กœ์ ํŠธ UI๋Š” ๊ต‰์žฅํžˆ ๊น”๋”ํ•œ ํŽธ์ด๊ธฐ ๋•Œ๋ฌธ์— ๋ฒ ์ด์งํ•œ ์ปดํฌ๋„ŒํŠธ๋ฅผ ์ ์šฉํ•˜๊ธฐ๋กœ..

 

 

MUI์—์„œ ์ œ๊ณตํ•˜๋Š” ํŽ˜์ด์ง€๋„ค์ด์…˜์—๋Š” ์น˜๋ช…์ ์ธ ๋‹จ์ ์ด ํ•˜๋‚˜ ์žˆ๋Š”๋ฐ, ํŽ˜์ด์ง€๊ฐ€ ๋งŽ์„ ๊ฒฝ์šฐ ์•„๋ž˜์™€ ๊ฐ™์ด ํŽ˜์ด์ง€๊ฐ€ ํ‘œ์‹œ๋œ๋‹ค๋Š” ์ ์ด๋‹ค.

๐Ÿค” ํŽ˜์ด์ง€๋ฅผ ์ด๋Ÿฐ์‹์œผ๋กœ ํ‘œ์‹œํ•œ๋‹ค๋‹ˆ? ์ข€ ๋‹นํ™ฉ์Šค๋Ÿฌ์›€..

 

์ด ๋•Œ๋ฌธ์— MUI๋ฅผ ์ด์šฉํ•˜์ง€ ์•Š๋Š” ๋ถ„๋“ค๋„ ๋งŽ์€ ๊ฒƒ ๊ฐ™๋‹ค. (๊ฒ€์ƒ‰ํ•ด ๋ณด๊ธฐ์—)
๊ทธ๋ž˜๋„ ์šฐ๋ฆฌ ํ”„๋กœ์ ํŠธ์˜ ๊ฒฝ์šฐ MUI์˜ ์ปดํฌ๋„ŒํŠธ๋“ค์„ ๋ถ€๋ถ„ ๋ถ€๋ถ„ ์ด์šฉํ•˜๊ณ  ์žˆ๊ณ , 10ํŽ˜์ด์ง€๊ฐ€ ๋„˜์–ด๊ฐˆ ์ •๋„์˜ ์•„์ฃผ ๋งŽ์€ ๋ฐ์ดํ„ฐ๊ฐ€ ์Œ“์ผ ๊ฒƒ ๊ฐ™์ง„ ์•Š๊ธฐ์—.. ์ผ๋‹จ ์ ์šฉํ•ด ๋ณด๊ธฐ๋กœ!

 

 

 

ํŽ˜์ด์ง€ ์•Œ๊ณ ๋ฆฌ์ฆ˜

ํŽ˜์ด์ง•์ฒ˜๋ฆฌ๋ฅผ ํ•˜๋ ค๋ฉด ํ•„์š”ํ•œ ๊ฒƒ๋“ค์ด ๋ช‡ ๊ฐ€์ง€ ์žˆ๋‹ค.

  • ํ•œ ํŽ˜์ด์ง€์— ๋ช‡ ๊ฐœ์˜ ๊ฒŒ์‹œ๊ธ€์„ ๋ณด์—ฌ์ค„ ๊ฒƒ์ธ์ง€?
  • ๋งˆ์ง€๋ง‰ ํŽ˜์ด์ง€๋Š” ๋ช‡ ํŽ˜์ด์ง€์ธ์ง€

 

๋‚˜์˜ ๊ฒฝ์šฐ์—๋Š” ํ•œ ํŽ˜์ด์ง€์— 5๊ฐœ์˜ ๊ฒŒ์‹œ๊ธ€์„ ๋ณด์—ฌ์ฃผ๊ธฐ๋กœ ํ–ˆ๋‹ค.
๊ทธ๋Ÿผ ์ดํŽ˜์ด์ง€์˜ ์ˆ˜ ๊ฐ™์€ ๊ฒฝ์šฐ๋Š” ์•„๋ž˜์™€ ๊ฐ™์ด ๊ณ„์‚ฐํ•ด๋ณผ ์ˆ˜ ์žˆ๋‹ค.

 const LAST_PAGE = board.length % 5 === 0 ? parseInt(board.length / 5) : parseInt(board.length / 5) + 1;

ํ•œ ํŽ˜์ด์ง€์— ๋“ค์–ด๊ฐˆ ์ˆ˜์ธ 5๋กœ ๋‚˜๋ˆ„์—ˆ์„ ๋•Œ ๋”ฑ ๋–จ์–ด์ง„๋‹ค๋ฉด ๊ทธ ์ˆ˜๊ฐ€ ๋งˆ์ง€๋ง‰ ํŽ˜์ด์ง€๊ฐ€ ๋  ๊ฒƒ์ด๊ณ ,
๊ทธ๋ ‡์ง€ ์•Š๋‹ค๋ฉด ๋‹ค์ŒํŽ˜์ด์ง€๋กœ ๋„˜์–ด๊ฐ€๋‹ˆ 1์„ ๋”ํ•ด์ฃผ๋Š” ์‹์ด๋‹ค.

 

 

๋‹ค์Œ์œผ๋กœ ์ด์ œ ๊ฐ๊ฐ ํŽ˜์ด์ง€์— ๋“ค์–ด๊ฐˆ ๋ฐ์ดํ„ฐ๋ฅผ ์ž˜๋ผ์„œ ๊ฐ€์ ธ์™€์•ผ ํ•œ๋‹ค.

  // ๊ฒŒ์‹œ๊ธ€ ๋ฆฌ์ŠคํŠธ ๋ Œ๋”๋ง ํ•ธ๋“ค๋Ÿฌ
  const handleBoardList = () => {
    ...
      return board
        .slice(5 * (currentPage - 1), 5 * (currentPage - 1) + 5)
        .map((item) => <BoardItem key={item.id} item={item} />);
    }
  };

๋‚˜์˜ ๊ฒฝ์šฐ์—๋Š” ์ „์ฒด ๋ฐ์ดํ„ฐ์—์„œ ํ˜„์žฌ ํŽ˜์ด์ง€์— ๋”ฐ๋ผ์„œ slice ํ•œ ๋ฐ์ดํ„ฐ๋“ค์„ ๊ฐ€์ ธ์™€์„œ ๋ฟŒ๋ ค์ฃผ๋„๋ก ํ–ˆ๋‹ค.


์ € ์‹์„ ๊ฐ๊ฐ์˜ ํŽ˜์ด์ง€์— ์ ์šฉํ•ด๋ณด๋ฉด ์ด๋ ‡๋‹ค.

// 1ํŽ˜์ด์ง€์ผ ๊ฒฝ์šฐ
5 * 0, 5 * 0 + 5 => ์ธ๋ฑ์Šค 0๋ถ€ํ„ฐ 5๊นŒ์ง€
// 2ํŽ˜์ด์ง€์ผ ๊ฒฝ์šฐ
5 * 1, 5 * 1 + 5 => ์ธ๋ฑ์Šค 5๋ถ€ํ„ฐ 10๊นŒ์ง€
...

 

 

 

MUI Pagination ์ปดํฌ๋„ŒํŠธ ์ ์šฉํ•˜๊ธฐ

์ ์šฉํ•œ ์ปดํฌ๋„ŒํŠธ๋Š” ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

<PaginationWrap>
   {currentLocation && (
        <Pagination page={currentPage} count={LAST_PAGE} defaultPage={1} onChange={handlePageChange}/>)}
</PaginationWrap>

Pagination ์ปดํฌ๋„ŒํŠธ์—๋Š” ์—ฌ๋Ÿฌ ์†์„ฑ๋“ค์ด ์žˆ๋‹ค.

  • page : ํ˜„์žฌ ํŽ˜์ด์ง€๊ฐ€ active
  • count : ๋งˆ์ง€๋ง‰ ํŽ˜์ด์ง€
  • defaultPage : ๋””ํดํŠธ ํŽ˜์ด์ง€ ๊ฐ’
  • onChange : ํŽ˜์ด์ง€ ๋ณ€๊ฒฝ๋  ์ˆ˜ ์žˆ๋„๋ก ๋„ฃ์–ด์ฃผ๋Š” ํ•จ์ˆ˜

 

onchange ํ•ธ๋“ค๋Ÿฌ์—๋Š” ํด๋ฆญํ•œ ๊ฐ’์„ ํ˜„์žฌ ํŽ˜์ด์ง€๋กœ ์„ค์ •ํ•  ์ˆ˜ ์žˆ๋Š” ๋กœ์ง์„ ๋„ฃ์–ด์ฃผ์—ˆ๋‹ค.

const [currentPage, setCurrentPage] = useState(1);
...
const handlePageChange = (event) => {
    const currentPageIndex = Number(event.target.outerText);
    setCurrentPage(currentPageIndex);
  };

 

 

์™„์„ฑํ•œ ๊ฒฐ๊ณผ๋ฌผ์€ ์•„๋ž˜์™€ ๊ฐ™๋‹ค!.. ๋ฐ์ดํ„ฐ๊ฐ€ ๋งŽ์ด ๋“ค์–ด๊ฐ€ ์žˆ์ง€ ์•Š์ง€๋งŒ 2ํŽ˜์ด์ง€๋กœ ์ด๋™ํ–ˆ์„ ๊ฒฝ์šฐ ๋ Œ๋”๋ง์ด ์ž˜ ๋œ๋‹ค. 

 

 

 

๋ฆฌ์•กํŠธ๋กœ ํŽ˜์ด์ง€๋„ค์ด์…˜์€ ์ฒ˜์Œ ๊ตฌํ˜„ํ•ด๋ณด์•˜๋Š”๋ฐ ํŽ˜์ด์ง• ๋กœ์ง์„ ์ดํ•ดํ•˜๊ธฐ๊นŒ์ง€ ์‹œ๊ฐ„์ด ์ข€ ๊ฑธ๋ ธ๋˜ ๊ฒƒ ๊ฐ™๋‹ค. ๐Ÿค”
๋‹ค์Œ์— ํŽ˜์ด์ง• ์ฒ˜๋ฆฌ๊ฐ€ ํ•„์š”ํ•  ๋•Œ๋„ ์š”๊ธดํ•˜๊ฒŒ ์“ธ ์ˆ˜ ์žˆ๊ธธ ๋ฐ”๋ผ๋ฉฐ!..

728x90