InfiniteScroll
An infinite scroll component that automatically loads more content as the user scrolls, with error handling and customizable loading states.
Basic Usage
Scrollable List
Scroll down to automatically load more items. Shows a spinner during loading and end message when all items are loaded.
import { InfiniteScroll } from 'fluxo-ui';
const [items, setItems] = useState(generateItems(0, 20));
const [hasMore, setHasMore] = useState(true);
const loadMore = async () => {
await new Promise((r) => setTimeout(r, 1000));
const newItems = generateItems(items.length, 20);
setItems((prev) => [...prev, ...newItems]);
if (items.length + 20 >= 100) setHasMore(false);
};
<InfiniteScroll loadMore={loadMore} hasMore={hasMore}>
{items.map((item) => (
<div key={item.id}>{item.title}</div>
))}
</InfiniteScroll>Error Handling
Pass an error string to show an error message with a retry button. Use onRetry to clear the error and re-attempt loading.
Error Handling with Retry
When loading fails, an error message with a retry button is displayed. Every other load attempt simulates a failure.
import { InfiniteScroll } from 'fluxo-ui';
const [error, setError] = useState<string | null>(null);
const loadMore = async () => {
await new Promise((r) => setTimeout(r, 800));
// Simulate random failures
if (Math.random() > 0.5) {
throw new Error('Network error');
}
setItems((prev) => [...prev, ...newItems]);
};
<InfiniteScroll
loadMore={loadMore}
hasMore={hasMore}
error={error}
onRetry={() => { setError(null); loadMore(); }}
/>Import
import { InfiniteScroll } from 'fluxo-ui';
import type { InfiniteScrollProps } from 'fluxo-ui';Props
loadMorereq() => Promise<void>Async function to load more data.
loadMorereq() => Promise<void>Async function to load more data.
hasMorereqbooleanWhether there is more data to load.
hasMorereqbooleanWhether there is more data to load.
isLoadingboolean"false"External loading state control.
isLoadingboolean"false"External loading state control.
errorstring | nullError message to display.
errorstring | nullError message to display.
onRetry() => voidCalled when the retry button is clicked.
onRetry() => voidCalled when the retry button is clicked.
thresholdnumber"200"Pixel distance from bottom to trigger loading.
thresholdnumber"200"Pixel distance from bottom to trigger loading.
loaderReactNodeCustom loading indicator.
loaderReactNodeCustom loading indicator.
endMessageReactNodeMessage shown when all data is loaded.
endMessageReactNodeMessage shown when all data is loaded.
errorMessageReactNodeCustom error display (overrides default).
errorMessageReactNodeCustom error display (overrides default).
scrollableTargetstring | HTMLElementID or element of the scrollable container.
scrollableTargetstring | HTMLElementID or element of the scrollable container.
inverseboolean"false"Load content at the top (chat-style).
inverseboolean"false"Load content at the top (chat-style).
childrenreqReactNodeList content to display.
childrenreqReactNodeList content to display.
classNamestringAdditional CSS class for the container.
classNamestringAdditional CSS class for the container.
Features
Intersection Observer
Uses IntersectionObserver for efficient scroll detection without scroll event listeners.
Error & Retry
Built-in error state display with a retry button to re-attempt loading.
Custom Loader
Replace the default spinner with any custom loading indicator.
Inverse Mode
Load content at the top of the container for chat-style interfaces.
Scrollable Target
Attach to any scrollable container by ID or element reference.
End Message
Display a custom message when all content has been loaded.