일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
- 오공완 #리액트 공부 #React
- map()에는 key값이 필요
- 오공완
- 클래스 추가하기 #특정 url 클래스 추가 #사이트 접속시 클래스 추가 #오공완 #javascript
- 리액트
- React
- 리스트랜더링
- Today
- Total
new_bird-hyun
React에서 대규모 데이터를 가상화와 페이징으로 처리하기 본문
// 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>
);
}
// 가상화와 페이징을 결합하면 대규모 데이터 렌더링과 탐색이 효율적임
'코딩 공부' 카테고리의 다른 글
서버 측에서 데이터를 점진적으로 불러오는 패턴 - React Streaming과 Suspense (0) | 2025.02.01 |
---|---|
React에서 무한 스크롤과 가상화를 결합하여 성능 최적화하기 (0) | 2025.01.31 |
React에서 가상화와 필터링 결합하기 (0) | 2025.01.13 |
React에서 대규모 데이터 필터링 및 성능 최적화 (0) | 2025.01.09 |
React Window로 동적 항목 높이와 컬럼 레이아웃 구현 (0) | 2025.01.08 |