Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

471
Views
React hooks window.scrollTo(0, 0) not working when overflow auto

I am using React at the moment, when I click on a button that takes me to another page, by default it goes to the bottom of my page. Tried using window.scrollTo(0, 0); but it doesn't seem to work.

const handleRedirection = () => {
    history.push(/home);
    window.scrollTo(0, 0);
  };

The page I would like to go to currently has css set as

height: calc(100vh - 50px);
overflow: scroll;

Was wondering is there a solution to fix this issue. Thanks! ๐Ÿ™

about 4 years ago ยท Juan Pablo Isaza
1 answers
Answer question

0

you can scroll to top based on route change

try this:

import { useLocation } from "react-router-dom";

//your codes code

  const { pathname } = useLocation();

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

//your codes

also, you can use it as a top-level component to apply this to all your pages

about 4 years ago ยท Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
ยฉ 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!