--- title: useInfiniteScroll description: "`useInfiniteScroll` is a custom hook that provides infinite scroll functionality." links: - source: https://github.com/yamada-ui/yamada-ui/tree/main/packages/react/src/hooks/use-infinite-scroll - storybook: https://yamada-ui.github.io/yamada-ui?path=/story/hooks-useinfinitescroll--basic --- ```tsx const [count, setCount] = useState(50) const { ref, finish } = useInfiniteScroll({ onLoad: ({ finish, index }) => { console.log("onLoad", index) setCount((prev) => prev + 50) if (index >= 5) finish() }, }) return ( {Array(count) .fill(0) .map((_, index) => ( 天元突破グレンラガン いいか、忘れんな。お前を信じろ。俺が信じるお前でもない。お前が信じる俺でもない。お前が信じる…お前を信じろ! ))} {!finish ? (
) : null}
) ``` ## Usage ```tsx import { useInfiniteScroll } from "@yamada-ui/react" ``` ```tsx import { useInfiniteScroll } from "@/components/ui" ``` ```tsx import { useInfiniteScroll } from "@workspace/ui" ``` ```tsx const { ref, finish } = useInfiniteScroll() ``` ### Specify the Viewport To specify the viewport, set a `ref` to `rootRef`. :::note If `rootRef` is not set, the browser's viewport will be used. ::: ```tsx const rootRef = useRef(null) const resetRef = useRef<() => void>(noop) const [count, setCount] = useState(50) const { ref, finish } = useInfiniteScroll({ resetRef, rootRef, onLoad: ({ finish, index }) => { console.log("onLoad", index) setCount((prev) => prev + 50) if (index >= 5) finish() }, }) return ( {Array(count) .fill(0) .map((_, index) => ( 天元突破グレンラガン いいか、忘れんな。お前を信じろ。俺が信じるお前でもない。お前が信じる俺でもない。お前が信じる…お前を信じろ! ))} {!finish ? (
) : null}
) ``` ### Set rootMargin To set [rootMargin](https://developer.mozilla.org/en-US/docs/Web/API/Intersection_Observer_API#rootmargin), assign a string to rootMargin. ```tsx const [count, setCount] = useState(50) const { ref, finish } = useInfiniteScroll({ rootMargin: "300px 0px 0px 0px", onLoad: ({ finish, index }) => { console.log("onLoad", index) setCount((prev) => prev + 50) if (index >= 5) finish() }, }) return ( {Array(count) .fill(0) .map((_, index) => ( 天元突破グレンラガン いいか、忘れんな。お前を信じろ。俺が信じるお前でもない。お前が信じる俺でもない。お前が信じる…お前を信じろ! ))} {!finish ? (
) : null}
) ``` ### Set threshold To set [threshold](https://developer.mozilla.org/en-US/docs/Web/API/Intersection_Observer_API#threshold), assign a number to `threshold`. ```tsx const [count, setCount] = useState(50) const { ref, finish } = useInfiniteScroll({ threshold: 1, onLoad: ({ finish, index }) => { console.log("onLoad", index) setCount((prev) => prev + 50) if (index >= 5) finish() }, }) return ( {Array(count) .fill(0) .map((_, index) => ( 天元突破グレンラガン いいか、忘れんな。お前を信じろ。俺が信じるお前でもない。お前が信じる俺でもない。お前が信じる…お前を信じろ! ))} {!finish ? (
) : null}
) ``` ### Initial Load To load initially, set `initialLoad` to `true`. By default, `initialLoad` is set to `false`, and the initial(`index=0`) `onLoad` is not executed. `true`: The first `onLoad` is executed regardless of the scroll amount, and the provided `index` starts from `0`.\ `false`: `onLoad` is executed when a certain scroll is reached, and the provided `index` starts from `1`. ```tsx const [count, setCount] = useState(50) const { ref, finish } = useInfiniteScroll({ initialLoad: true, onLoad: ({ finish, index }) => { console.log("onLoad", index) setCount((prev) => prev + 50) if (index >= 5) finish() }, }) return ( {Array(count) .fill(0) .map((_, index) => ( 天元突破グレンラガン いいか、忘れんな。お前を信じろ。俺が信じるお前でもない。お前が信じる俺でもない。お前が信じる…お前を信じろ! ))} {!finish ? (
) : null}
) ``` ### Change the Starting index To change the starting index, set a number to `startIndex`. The default is `1`. ```tsx const [count, setCount] = useState(50) const { ref, finish } = useInfiniteScroll({ startIndex: 3, onLoad: ({ finish, index }) => { console.log("onLoad", index) setCount((prev) => prev + 50) if (index >= 5) finish() }, }) return ( {Array(count) .fill(0) .map((_, index) => ( 天元突破グレンラガン いいか、忘れんな。お前を信じろ。俺が信じるお前でもない。お前が信じる俺でもない。お前が信じる…お前を信じろ! ))} {!finish ? (
) : null}
) ``` ### Reverse To reverse, set `reverse` to `true`. The default is `false`. ```tsx const rootRef = useRef(null) const [count, setCount] = useState(50) const { ref, finish } = useInfiniteScroll({ reverse: true, rootRef, onLoad: ({ finish, index }) => { console.log("onLoad", index) setCount((prev) => prev + 50) if (index >= 5) finish() }, }) return ( {!finish ? (
) : null} {Array(count) .fill(0) .map((_, index) => ( 天元突破グレンラガン いいか、忘れんな。お前を信じろ。俺が信じるお前でもない。お前が信じる俺でもない。お前が信じる…お前を信じろ! ))}
) ```