Hooks
Functions
useInfiniteScroll
useInfiniteScroll hook provides a convenient way to implement infinite scrolling in a React component.
Example
import useInfiniteScroll from '@/utils/hooks/useInfiniteScroll';
const InfiniteScrollComponent = () => {
const [items, setItems] = useState([...Array(20).keys()]);
const loadMoreItems = async () => {
await new Promise((resolve) => setTimeout(resolve, 1000));
setItems((prevItems) => [...prevItems, ...Array(20).keys()]);
};
const { isLoading, containerRef } = useInfiniteScroll({
onLoadMore: loadMoreItems,
});
return (
<div ref={containerRef} style={{ height: '300px', overflowY: 'auto' }}>
{items.map((item, index) => (
<div key={index} style={{ padding: '10px', border: '1px solid #ddd' }}>
Item {index + 1}
</div>
))}
{isLoading && <p>Loading more items...</p>}
</div>
);
};
export default InfiniteScrollComponent;
Params
| param | Description | Type | Default |
|---|---|---|---|
| offset | The offset from the bottom of the container at which to trigger the load more action. | string | '0px' |
| shouldStop | Flag to stop the infinite scroll from triggering further load more actions. | boolean | false |
| onLoadMore | The function to be called when the user reaches the bottom of the container. | () => Promise | undefined |
Return
| return | Description | Type | Default |
|---|---|---|---|
| isLoading | Indicates whether the hook is currently loading more content. | boolean | |
| containerRef | A ref callback function to be assigned to the container that requires infinite scrolling. | LegacyRef |