loading

How to fix, Next.js link doesn’t render with the page scrolled at the top

By default next js scroll at the top when navigating to another page or link, there is also a way by which you can stop scrolling when navigating to another page or URL.

Check whether your layout is not set to overflow:auto or max-height : 100vh something due to which it’s not scrolling to the top because the content is just set to max height 100vh.

join WhatsApp group

there are the following props for the link component in next js which can be used

Href is the path of the page or which you want to navigate.

This is used to set whether the page should be scrolled at the top or not after navigating

Shallow is used to update the path of the current page without rerunning getStaticProps, getServerSideProps, or getInitialProps. its default value is false.

You can add a prefetch prop if you want to prefetch that page in the background. The link is by default prefetched.

You can also use the following ways to solve this scrolling problem.

Use router events to scroll the content to the top of the navigation complete.

import "@/styles/globals.css";

import { useRouter } from 'next/router'
export default function App({ Component, pageProps }) {

  const router=useRouter()
  useEffect(()=>{
   Router.events.on("routeChangeComplete", () => {
    window.scrollTo(0,0)
    });
   },[])
  return <Component {...pageProps} />;
}

Loading...

About Author

Loading...

Loading...

👨‍💻

Frontend development

Learn frontend development with examples and practical exercise of HTML,CSS,JS,React.js.