Replies: 2 comments 4 replies
-
Well, this is my best attempt. Unfortunately, the throttle loses some range changes though. Does anyone else have other ideas? import { useThrottledCallback } from "@mantine/hooks";
import { useRef } from "react";
import { VListHandle } from "virtua";
export function useRangeChange(
virtuaHandleRef: React.RefObject<VListHandle>,
onRangeChange: (startIndex: number, endIndex: number) => void,
) {
const startIndexRef = useRef(-1);
const endIndexRef = useRef(-1);
return useThrottledCallback(function onScroll() {
const virtuaHandle = virtuaHandleRef.current;
if (!virtuaHandle) return;
const { startIndex, endIndex } = virtuaHandle;
if (
startIndex !== startIndexRef.current ||
endIndex !== endIndexRef.current
) {
onRangeChange(startIndex, endIndex);
}
startIndexRef.current = startIndex;
endIndexRef.current = endIndex;
}, 200);
} |
Beta Was this translation helpful? Give feedback.
-
Throttling will improve perf, but I think it's not necessary for most cases. In theory, React will render virtualizer once (sometimes batched or called twice for bailout though) on each scroll event. The The Yes, of course throttling is better if it fits the usecase. And maybe we can implement memoization for |
Beta Was this translation helpful? Give feedback.
-
Hi!
I used
onRangeChange
extensively with voyager. https://github.com/aeharding/voyager The main purpose was to know when an item has been scrolled past, to mark as read.Now I see it is recommended to call handle.startIndex/endIndex.
However doesn't that call
findIndex
which isn't efficient to be calledonScroll
? I don't want to throttle if I can avoid it, because that might miss quickly scrolling down and up again.I just want to check what is recommended. I try to avoid scroll linked computations for performance reasons :)
Beta Was this translation helpful? Give feedback.
All reactions