Skip to main content

useInfiniteScroll

Infinite scrolling of the element

Usage

Live Editor

function Demo() {
  const ref = useRef<HTMLDivElement>(null);
  const [data, setData] = useState([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);

  useInfiniteScroll(
    ref,
    () => {
      const length = data.length + 1;
      const newData = data.slice();
      if (newData.length === 400) {
        return;
      }
      newData.push(...Array.from({ length: 5 }, (_, i) => length + i));
      setData(newData);
    },
    { distance: 10 },
  );

  return (
    <div>
      <div ref={ref} style={{ width: 300, height: 300, overflow: "scroll" }}>
        {data.map(item => (
          <div key={item} style={{ padding: 12, border: "1px solid" }}>
            item-{item}
          </div>
        ))}
      </div>
    </div>
  );
};

Result
Loading...

API

useInfiniteScroll

Returns

void

Arguments

ArgumentDescriptionTypeDefaultValue
targetdom elementBasicTarget<Element> (Required)-
onLoadMoreload more functionUseInfiniteScrollLoadMore (Required)-
optionsoptional paramsUseInfiniteScrollOptions | undefined-

UseInfiniteScrollLoadMore

Returns

void | Promise<void>

Arguments

ArgumentDescriptionTypeDefaultValue
statethe return state of useScrollreadonly [number, number, boolean, UseInfiniteScrollArrivedState, UseInfiniteScrollDirection] (Required)-

UseInfiniteScrollOptions

PropertyDescriptionTypeDefaultValue
distanceThe minimum distance between the bottom of the element and the bottom of the viewportnumber0
directionThe direction in which to listen the scroll.'top' | 'bottom' | 'left' | 'right''bottom'
preserveScrollPositionWhether to preserve the current scroll position when loading more items.boolean-
throttleThrottle time for scroll event, it’s disabled by default.number0
idleThe check time when scrolling ends.This configuration will be setting to (throttle + idle) when the throttle is configured.number-
offsetOffset arrived states by x pixelsUseScrollOffset-
onScrollTrigger it when scrolling.(e: Event) => void-
onStopTrigger it when scrolling ends.(e: Event) => void-
eventListenerOptionsListener options for scroll event.boolean | AddEventListenerOptions{capture: false, passive: true}

UseInfiniteScrollArrivedState

PropertyDescriptionTypeDefaultValue
leftarrived leftboolean (Required)-
rightarrived rightboolean (Required)-
toparrived topboolean (Required)-
bottomarrived bottomboolean (Required)-

UseInfiniteScrollDirection

PropertyDescriptionTypeDefaultValue
leftscroll leftboolean (Required)-
rightscroll rightboolean (Required)-
topscroll topboolean (Required)-
bottomscroll bottomboolean (Required)-

BasicTarget

export type BasicTarget<T extends TargetType = Element> = (() => TargetValue<T>) | TargetValue<T> | MutableRefObject<TargetValue<T>>;

TargetValue

type TargetValue<T> = T | undefined | null;

TargetType

type TargetType = HTMLElement | Element | Window | Document | EventTarget;

UseScrollOffset

export interface UseScrollOffset {
left?: number;
right?: number;
top?: number;
bottom?: number;
}