Command palette

@yasinsensoy/web

Web · Components

InfiniteScroll

Wrapper that loads the next page when the end of the list is reached: feeds, product lists, comments, chat history.

5 exports

Live example

Preview

Post 1
Post 2
Post 3
Post 4
Post 5
Post 6
Post 7
Post 8
Post 9
Post 10

Code

import { InfiniteScroll, Post } from "@yasinsensoy/web";

<InfiniteScroll scrollable className="h-96" onLoadMore={nextPage} hasMore={dahaVar} endMessage>
  {gonderiler.map((g) => <Post key={g.id} {...g} />)}
</InfiniteScroll>

Events

  1. Events appear here when you interact with the preview.

Import

import { InfiniteScroll, useLoadMore } from "@yasinsensoy/web";

InfiniteScroll

componentClient component

Wrapper that loads the next page when the end of the list is reached: feeds, product lists, comments, chat history. Give the items as children; for tens of thousands of items use VirtualList (it has the same onLoadMore behavior).

  • It doesn't listen to scroll events; it watches for nearing the end with IntersectionObserver and starts loading threshold ahead.
  • One load at a time; if the page doesn't fill the screen, the next one loads too.
  • On error it doesn't loop; a "Try again" button appears.
  • direction="up": the scroll position is kept for items added at the start.
  • manualAfter: switches to a button after a few automatic loads; there is a "more" path for keyboard and screen reader users too.
  • aria-busy is reported while loading.
InfiniteScroll({ children, onLoadMore, onError, hasMore, loading, direction, threshold, manualAfter, loader, endMessage, errorMessage, loadMoreLabel, scrollable, scrollElement, className, style, ...props }: InfiniteScrollProps): JSX.Element

Examples

const { data, fetchNextPage, hasNextPage } = useInfiniteQuery(…);
<InfiniteScroll onLoadMore={fetchNextPage} hasMore={hasNextPage} endMessage>
  {data.pages.flat().map((post) => <PostCard key={post.id} post={post} />)}
</InfiniteScroll>

<InfiniteScroll direction="up" onLoadMore={olderMessages} hasMore={hasMore}
  scrollable className="h-96">{messages}</InfiniteScroll>

useLoadMore

function

Hook that loads the next page when nearing the end of the list; the foundation of InfiniteScroll and VirtualList. Pass the returned sentinel to the ref of an empty element at the end of the list.

  • It doesn't listen to scroll events; it uses IntersectionObserver.
  • One load at a time; if the list still doesn't fill the screen when a load finishes, the next page loads too.
useLoadMore({ onLoadMore, onError, hasMore, root, threshold, loading, auto, }: UseLoadMoreOptions): {
    sentinel: Dispatch<SetStateAction<Element | null>>

Examples

const { sentinel, status, load } = useLoadMore({ onLoadMore: nextPage, hasMore });
<ul>{…}<li ref={sentinel} aria-hidden /></ul>

Props of the InfiniteScroll component.

Extends: Omit<ComponentProps<"div">, "children">

NameTypeDefaultDescription
children?ReactNodeItems loaded so far.
onLoadMore() => unknownLoads the next page. If it returns a Promise, it isn't called again until it settles; if it throws, a "Try again" button appears.
onError?(error: unknown) => voidCalled when onLoadMore fails (logging, notice etc.). The error is caught and a "Try again" button is shown; it doesn't reach the console as an uncaught error.
hasMorebooleanAre there more pages to load?
loading?booleanLoading state from outside (e.g. the first page); doesn't trigger while true.
direction?"down" | "up""down"Load direction. "up": like a chat; older items are added to the start of the list and the scroll position is kept (the content doesn't jump).
threshold?number400Start loading this far from the end (px).
manualAfter?numberAfter this many automatic loads it switches to a "Load more" button, so the page footer can be reached. 0 means always a button. If omitted, it always loads automatically.
loader?ReactNodeA spinnerLoading indicator.
endMessage?ReactNodeShown at the end of the list when everything is loaded. true shows the built-in text.
errorMessage?ReactNode{@link UIProvider} textText shown when loading fails.
loadMoreLabel?ReactNode{@link UIProvider} textText of the "Load more" button.
scrollable?booleanfalseThe list scrolls in its own box (a chat window, side panel). Give the height with className (h-96, max-h-[60vh], flex-1). The library's overlay scrollbar is used (see ScrollArea); you don't need to write overflow-y-auto. If omitted, the nearest scrollable ancestor (e.g. a ScrollArea) scrolls, or the page if none.
scrollElement?() => HTMLElement | nullScroll area to observe. If omitted, the nearest scrollable ancestor, or the page if none.

Loading state.

type LoadMoreStatus = "idle" | "loading" | "error" | "done"

Options of the useLoadMore hook.

NameTypeDefaultDescription
onLoadMore(() => unknown) | undefinedLoads the next page. If it returns a Promise, it isn't called again until it settles; if it throws (or the Promise rejects), the error is caught and the status becomes error: it doesn't reach the console/dev overlay as an uncaught error.
onError?(error: unknown) => voidCalled when onLoadMore fails (logging, notice etc.). The error is already caught and shown as "Try again" in the UI; don't rethrow it here.
hasMorebooleanAre there more pages to load? Not observed while false.
root?() => Element | nullScroll area in which the sentinel element (the end of the list) is observed. null is the page itself. If omitted, the sentinel's nearest scrollable ancestor is found.
threshold?number400Start loading this far from the end (px).
loading?booleanLoading state from outside (e.g. the first page); doesn't trigger while true.
auto?booleantrueIf false, the sentinel is not observed (e.g. "Load more" button mode).