Infinite Scroll

An Infinite Scroll component is a user interface feature that automatically loads and displays additional content as the user scrolls down a page. Instead of traditional pagination, this component creates a seamless browsing experience by continuously appending new items, such as images or articles, when the user reaches the bottom of the viewport. This enhances user engagement and keeps the content flow uninterrupted, making it ideal for applications like social media feeds, product galleries, and news websites.

Demo

Item 0
Item 1
Item 2
Item 3
Item 4
Item 5
Item 6
Item 7
Item 8
Item 9
Item 10
Item 11
Item 12
Item 13
Item 14
Item 15
Item 16
Item 17
Item 18
Item 19

Usage

import { NCard, NInfiniteScroll, NLoading } from '@nayan-ui/react';
import React, { useState } from 'react';
const InfiniteScroll = () => {
const [items, setItems] = useState(new Array(20).fill(''));
const [isFetching, setIsFetching] = useState(false);
const fetchNextPage = () => {
setIsFetching(true);
setTimeout(() => {
const newItems = [...items, ...new Array(20).fill('')];
setItems(newItems);
setIsFetching(false);
}, 2000);
};
return (
<NInfiniteScroll
next={() => !isFetching && fetchNextPage()}
hasMore={true}
loader={<NLoading />}
dataLength={items.length}
scrollThreshold={0.99}>
{items.map((item: any, index: number) => (
<NCard className="p-3 mb-3">Item {index}</NCard>
))}
</NInfiniteScroll>
);
};
export default InfiniteScroll;

Attributes

NameTypeDefaultDetails
nextfunctionRequiredFunction to load next set of data.
hasMorebooleanRequiredWhether there is more data to load.
childrenReact.ReactNodeRequiredContent to display in the scroll container.
loaderReact.ReactNodeRequiredLoading indicator component.
scrollThresholdnumber | stringOptionalThreshold for triggering next load.
endMessageReact.ReactNodeOptionalMessage to show when no more data.
styleCSSPropertiesOptionalCustom styles for the container.
heightnumber | stringOptionalHeight of the scroll container.
scrollableTargetHTMLElement | string | nullOptionalTarget element for scrolling.
hasChildrenbooleanOptionalWhether container has children.
inversebooleanOptionalWhether to use inverse scrolling.
pullDownToRefreshbooleanOptionalEnable pull to refresh functionality.
pullDownToRefreshContentReact.ReactNodeOptionalContent for pull to refresh.
releaseToRefreshContentReact.ReactNodeOptionalContent for release to refresh.
pullDownToRefreshThresholdnumberOptionalThreshold for pull to refresh.
refreshFunctionfunctionOptionalFunction to call on refresh.
onScroll(e: Event) => anyOptionalScroll event handler.
dataLengthnumberRequiredLength of current data array.
initialScrollYnumberOptionalInitial scroll position.
classNamestring' 'You can customise by passing tailwind classes.
aria-labelstringOptionalARIA label for accessibility.