Next.js – router.push without scrolling to the top

router.push has a scroll option, it is true by default. You can turn it off like this:

const router = useRouter();

async function navigate(newCurrency) {
  router.push({
    pathname: router.pathname,
    query: { ...router.query, currency: newCurrency.value },
  }, undefined, { scroll: false });
}

router.push accepts the most of (if not all) next/link‘s props in the options object. You can check them here: https://nextjs.org/docs/api-reference/next/link

Leave a Comment