Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Scroll to top on route change #132

Closed
notangelmario opened this issue Oct 6, 2020 · 4 comments
Closed

Scroll to top on route change #132

notangelmario opened this issue Oct 6, 2020 · 4 comments

Comments

@notangelmario
Copy link

How can we make the page scroll back to top on route change? Is there a function we can hook into the Link component? Some kind of option or something

@molefrog
Copy link
Owner

molefrog commented Oct 6, 2020

@notangelmario The solution should probably similar to what they recommend in react-router: https://reactrouter.com/web/guides/scroll-restoration/scroll-to-top

import { useEffect } from "react";
import { useLocation } from "wouter";

export default function ScrollToTop() {
  const [ pathname ] = useLocation();

  useEffect(() => {
    window.scrollTo(0, 0);
  }, [pathname]);

  return null;
}

@notangelmario
Copy link
Author

Thank you very much!

@xav-ie
Copy link

xav-ie commented Nov 9, 2022

Just wanted to note that the solution above is outdated:

import { useEffect } from "react";
import { useLocation } from "wouter";

export default function ScrollToTop() {
  const  [ pathname ] = useLocation();

  useEffect(() => {
    window.scrollTo(0, 0);
  }, [pathname]);

  return null;
}

Simply change the curly braces to square brackets and you are set!

@molefrog
Copy link
Owner

molefrog commented Nov 13, 2022

I've updated my comment, thanks for spotting the typo @xav-ie

JannekS added a commit to JannekS/hcs2023-travelblog-react that referenced this issue Jun 10, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants