|
1 | | -import React from 'react'; |
| 1 | +import { useEffect, useState, useCallback, useRef } from 'react'; |
| 2 | +import { WindowScroller, CellMeasurer, CellMeasurerCache, AutoSizer, List, ListRowProps } from 'react-virtualized'; |
| 3 | +import { checkInfiniteScrollPosition } from '../../helpers/scroll'; |
| 4 | +import { throttle } from 'lodash-es'; |
| 5 | + |
| 6 | +import { ImageListItem } from '../ImageList'; |
| 7 | +import { Container, Heading, Button, Text } from '@chakra-ui/react'; |
| 8 | +import StackSkleton from '../../components/StackSkeleton'; |
| 9 | + |
| 10 | +import './index.scss'; |
| 11 | + |
| 12 | +const cellCache = new CellMeasurerCache({ |
| 13 | + defaultWidth: 100, |
| 14 | + fixedWidth: true, |
| 15 | +}); |
| 16 | + |
| 17 | +let totalList: ImageListItem[] = []; |
2 | 18 |
|
3 | 19 | const ImageListVirtualized = () => { |
4 | | - return <div>ImageListVirtualized</div>; |
| 20 | + const [list, setList] = useState<ImageListItem[]>([]); |
| 21 | + const listRef = useRef<List>(null); |
| 22 | + |
| 23 | + const rowRenderer = ({ index, key, parent, style }: ListRowProps) => { |
| 24 | + return ( |
| 25 | + <CellMeasurer cache={cellCache} parent={parent} key={key} columnIndex={0} rowIndex={index}> |
| 26 | + {({ measure }) => ( |
| 27 | + <div style={style} key={index}> |
| 28 | + <div className="image-list-item"> |
| 29 | + <section className="thumb-wrap"> |
| 30 | + <img src={list[index].thumbnailUrl} alt="" onLoad={measure} /> |
| 31 | + </section> |
| 32 | + <section> |
| 33 | + <p>index: {index}</p> |
| 34 | + <p>title: {list[index].title}</p> |
| 35 | + </section> |
| 36 | + </div> |
| 37 | + </div> |
| 38 | + )} |
| 39 | + </CellMeasurer> |
| 40 | + ); |
| 41 | + }; |
| 42 | + |
| 43 | + const fetchData = useCallback(async () => { |
| 44 | + // const res = await fetch('https://jsonplaceholder.typicode.com/photos'); |
| 45 | + // console.log('res :>> ', res); |
| 46 | + fetch('https://jsonplaceholder.typicode.com/photos').then((res) => { |
| 47 | + const data = res.json(); |
| 48 | + |
| 49 | + data.then((newList) => { |
| 50 | + totalList = newList; |
| 51 | + addList(); |
| 52 | + }); |
| 53 | + }); |
| 54 | + }, []); |
| 55 | + |
| 56 | + const addList = useCallback(() => { |
| 57 | + if (!totalList.length) { |
| 58 | + return; |
| 59 | + } |
| 60 | + |
| 61 | + const data = totalList.splice(0, 100); |
| 62 | + setList([...list, ...data]); |
| 63 | + }, [list]); |
| 64 | + |
| 65 | + const onScroll = useCallback(() => { |
| 66 | + const isNeedFetching = checkInfiniteScrollPosition({ bottom: 600 }); |
| 67 | + if (isNeedFetching) { |
| 68 | + addList(); |
| 69 | + } |
| 70 | + }, [addList]); |
| 71 | + |
| 72 | + useEffect(() => { |
| 73 | + fetchData(); |
| 74 | + }, [fetchData]); |
| 75 | + |
| 76 | + useEffect(() => { |
| 77 | + const onScrollTrottle = throttle(onScroll, 100); |
| 78 | + window.addEventListener('scroll', onScrollTrottle); |
| 79 | + return () => window.removeEventListener('scroll', onScrollTrottle); |
| 80 | + }, [onScroll]); |
| 81 | + |
| 82 | + return ( |
| 83 | + <> |
| 84 | + <Container padding={0} marginBottom={5} textAlign="center"> |
| 85 | + <Heading size="md" mb={5} textAlign="center"> |
| 86 | + react-virtualized (O) |
| 87 | + </Heading> |
| 88 | + <Button mb={5} colorScheme="blue" onClick={addList}> |
| 89 | + 목록 추가하기 |
| 90 | + </Button> |
| 91 | + <Text fontSize="20px" color="tomato"> |
| 92 | + 현재목록: {list.length} 개 |
| 93 | + </Text> |
| 94 | + </Container> |
| 95 | + |
| 96 | + <section> |
| 97 | + {list.length ? ( |
| 98 | + <WindowScroller> |
| 99 | + {({ height, scrollTop, isScrolling, onChildScroll }) => ( |
| 100 | + <AutoSizer disableHeight> |
| 101 | + {({ width }) => ( |
| 102 | + <List |
| 103 | + ref={listRef} |
| 104 | + autoHeight |
| 105 | + height={height} |
| 106 | + width={width} |
| 107 | + isScrolling={isScrolling} |
| 108 | + overscanRowCount={0} |
| 109 | + onScroll={onChildScroll} |
| 110 | + scrollTop={scrollTop} |
| 111 | + rowCount={list.length} |
| 112 | + rowHeight={cellCache.rowHeight} |
| 113 | + rowRenderer={rowRenderer} |
| 114 | + deferredMeasurementCache={cellCache} |
| 115 | + /> |
| 116 | + )} |
| 117 | + </AutoSizer> |
| 118 | + )} |
| 119 | + </WindowScroller> |
| 120 | + ) : ( |
| 121 | + <StackSkleton count={5} /> |
| 122 | + )} |
| 123 | + </section> |
| 124 | + </> |
| 125 | + ); |
5 | 126 | }; |
6 | 127 |
|
7 | 128 | export default ImageListVirtualized; |
0 commit comments