Fetching & Displaying Data

How should large datasets be handled when displaying them in the UI?
function VirtualizedList({ items }) {
  const [visibleItems, setVisibleItems] = useState([]);
  const containerRef = useRef(null);

  useEffect(() => {
    const handleScroll = () => {
      const { scrollTop, clientHeight } = containerRef.current;
      const startIndex = Math.floor(scrollTop / 50);
      const endIndex = Math.floor((scrollTop + clientHeight) / 50);
      setVisibleItems(items.slice(startIndex, endIndex + 1));
    };

    containerRef.current.addEventListener('scroll', handleScroll);
    return () => containerRef.current?.removeEventListener('scroll', handleScroll);
  }, [items]);
Next Question (9/20)