Next.js: window.scrollTo(0, 0) doesn't work

Created on 27 Sep 2018  路  8Comments  路  Source: vercel/next.js

Bug report

I added this code to _app.js

Router.events.on('routeChangeComplete', () => {
  NProgress.done()
  window.scrollTo(0, 0)
})

Everything ok, but scroll doesn't work

All 8 comments

next/link already implements this behavior, why'd you need to trigger it another time 馃

Nothing, after each transition my page doesn't scroll to top. I'm using Nextjs Canary 7*

Can you create a minimal reproduction?

@timneutkens Is there any solution for the scroll position; when you refresh the page or click to history back button on your browser?

I'm not sure what you're referring to exactly, could you create a minimal reproduction?

My project is working fine
the example in _app.tsx

  public componentDidMount() {
    const previousCallback = Router.onRouteChangeComplete;
    Router.onRouteChangeComplete = (url: string) => {
      if (typeof previousCallback === "function") {
        previousCallback(url);
      }

      window && typeof window === "object" && window.scrollTo && typeof window.scrollTo === "function" && window.scrollTo(0, 0);
    };
  }

Going to close this

Can you create a minimal reproduction?

@timneutkens Could you provide a minimal reproduction of a working example? Because next/Link is not scrolling to top for me either.

Reference Code

Was this page helpful?
0 / 5 - 0 ratings

Related issues

lixiaoyan picture lixiaoyan  路  3Comments

formula349 picture formula349  路  3Comments

wagerfield picture wagerfield  路  3Comments

irrigator picture irrigator  路  3Comments

sospedra picture sospedra  路  3Comments