Notice
Recent Posts
Recent Comments
Link
«   2025/05   »
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
Archives
Today
Total
관리 메뉴

new_bird-hyun

React에서 대규모 데이터를 가상화와 페이징으로 처리하기 본문

코딩 공부

React에서 대규모 데이터를 가상화와 페이징으로 처리하기

새혀니 2025. 1. 20. 22:54

// VirtualizedPagination.js

import React, { useState } from 'react';
import { FixedSizeList as List } from 'react-window';

const allItems = Array.from({ length: 10000 }, (_, i) => `Item ${i + 1}`);
const PAGE_SIZE = 100;

export default function VirtualizedPagination() {
  const [currentPage, setCurrentPage] = useState(0);

  const startIdx = currentPage * PAGE_SIZE;
  const endIdx = startIdx + PAGE_SIZE;
  const items = allItems.slice(startIdx, endIdx);

  const totalPages = Math.ceil(allItems.length / PAGE_SIZE);

  return (
    <div>
      <h1>가상화와 페이징</h1>
      <List
        height={400}
        itemCount={items.length}
        itemSize={35}
        width="100%"
      >
        {({ index, style }) => (
          <div style={{ ...style, borderBottom: '1px solid #ddd', padding: '5px' }}>
            {items[index]}
          </div>
        )}
      </List>
      <div style={{ marginTop: '10px' }}>
        <button
          onClick={() => setCurrentPage((prev) => Math.max(prev - 1, 0))}
          disabled={currentPage === 0}
        >
          이전
        </button>
        <span style={{ margin: '0 10px' }}>
          {currentPage + 1} / {totalPages}
        </span>
        <button
          onClick={() => setCurrentPage((prev) => Math.min(prev + 1, totalPages - 1))}
          disabled={currentPage === totalPages - 1}
        >
          다음
        </button>
      </div>
    </div>
  );
}

 

// 가상화와 페이징을 결합하면 대규모 데이터 렌더링과 탐색이 효율적임