Rehooks
Content

useScroll

Custom hook that tracks and returns the current scroll position, and provides a method to scroll to specific coordinates.

useScoll

Usage

Component.tsx
import { useScroll } from "~/hooks/useScroll";
 
function Component() {
  const { position, scrollTo } = useScroll();
 
  return (
    <div>
      <h1>Scroll Position</h1>
      <p>X: {position.x}</p>
      <p>Y: {position.y}</p>
      <button onClick={() => scrollTo({ top: 0 })}>Scroll to top</button>
    </div>
  );
}

API

useScroll

function useScroll(): {
  position: ScrollPosition;
  scrollTo: (options: ScrollToOptions) => void;
};

Returns

NameTypeDescription
positionScrollPositionThe current scroll position.
scrollTo(options: ScrollToOptions) => voidA function to scroll to a specific position.

On this page