diff --git a/src/App.css b/src/App.css new file mode 100644 index 0000000..5e617bc --- /dev/null +++ b/src/App.css @@ -0,0 +1,15 @@ +.container { +/* display: flex; + flex-direction: column; + flex-wrap: nowrap;*/ + height: 500px; + overflow: auto; + border: 1px solid black; +} +.item { + font-size: 16pt; + border: 1px dotted gray; + /*margin: 5px 10px; + padding: 10px;*/ + border-radius: 5px; +} \ No newline at end of file diff --git a/src/App.jsx b/src/App.jsx index 531f9c5..8fb1e68 100644 --- a/src/App.jsx +++ b/src/App.jsx @@ -1,45 +1,33 @@ -import { useState, useRef } from 'react' -import './App.scss' +import { useEffect, useRef, useState } from 'react'; +import styles from './App.css'; -function VirtualList({ items, itemHeight=50, containerHeight=400 }) { - const containerRef = useRef(null) - //Положение большого нивидимого контейнера - const [scrollTop,setScrollTop] = useState(0); - //Количество отображаемых элементов - const visibleCount = Math.ceil(containerHeight / itemHeight); - //Общая высота нивидимого контейнера - const totalHeight = items.length * itemHeight; - //Начало и конец подгружаемых(отображаемых) элементов - const startIndex = Math.floor(scrollTop / itemHeight); - const endIndex = Math.min(startIndex + visibleCount + 1, items.length); - //Срез видимых элементов из общего списка - const visibleItems = items.slice(startIndex,endIndex); - - const handleScroll = e => { - setScrollTop(e.target.scrollTop); - console.log(e.target.scrollTop) - } - - return ( -
-
-
- {visibleItems.map(item => ( -
- {item.name} -
- ))} -
-
-
- ) -} -/* +const InfiniteList = () => { + const [items, setItems] = useState([...Array(20).keys()]); // Initial data + const loaderRef = useRef(null); + + const loadMore = () => { + setItems((prev) => [...prev, ...Array(10).keys()].map((_, i) => prev.length + i)); + }; + + useEffect(() => { + const observer = new IntersectionObserver((entries) => { + if (entries[0].isIntersecting) { + loadMore(); // Load next batch when the loader is seen + } + }); + + if (loaderRef.current) observer.observe(loaderRef.current); + return () => observer.disconnect(); + }, []); + + return ( + + ); +}; - -*/ function App() { const generatedBigDataSet = () => { return Array.from({ length: 100 }, (_, index) => ({ @@ -54,7 +42,9 @@ function App() { <>

Визуализация большого списка

- +
+ +
) diff --git a/src/App.scss b/src/App.scss index bae0684..5e617bc 100644 --- a/src/App.scss +++ b/src/App.scss @@ -2,6 +2,7 @@ /* display: flex; flex-direction: column; flex-wrap: nowrap;*/ + height: 500px; overflow: auto; border: 1px solid black; }