michaeljscript / next-restore-scroll-position

Restore scroll position using NextJS
15 stars 2 forks source link

Restore scroll position in NextJS

Restores scroll position when navigating back and forth in NextJS.

Installation

yarn add next-restore-scroll-position
npm i next-restore-scroll-position

Usage

import { useScrollRestoration } from 'next-restore-scroll-position';

function App() { // This needs to be NextJS App Component
    const router = useRouter();
    useScrollRestoration(router);
}

You can also disable the scroll restoration by passing enabled property

import { useScrollRestoration } from 'next-restore-scroll-position';

function App() { // This needs to be NextJS App Component
    const router = useRouter();
    useScrollRestoration(router, {enabled: false});
}

Peer dependencies