0

I'm pretty new on React JS. I saw a lot of similar questions about getting the top of the page with React Router, etc. But my question is not about React Router, it's just about React JS itself.

I'm working on a one page, and I'm wondering how can I get the top of my page every time I refresh this page. I mean, when I refresh the page, I'm still at the same place. I tried this :

componentDidMount(){
  window.scrollTo(0, 0);
}

But it does not working. Of course, I tried all things people recommended on similar subjects.

Any other ideas ?

SecretAgentMan
  • 2,856
  • 7
  • 21
  • 41
Laurie Vince
  • 123
  • 2
  • 2
  • 8
  • Did you check this one out yet? https://stackoverflow.com/questions/33188994/scroll-to-the-top-of-the-page-after-render-in-react-js – Bumhan Yu Jan 24 '20 at 17:44
  • Can you confirm that the componentDidMount life Cycle method is running? like try to do a console.log("working") and if see it its being called – harisu Jan 24 '20 at 18:53
  • Hey Harisu, this was the first thing I made ! And it works with console.log(). – Laurie Vince Jan 25 '20 at 14:11

1 Answers1

0

you can use it like this

import React,{Component} from 'react';


class App extends Component{
    componentDidMount(){
        this.wrapperNode.current.scrollTo(0,0)
    }
    render(){
        return <div ref={node=>this.wrapperNode = node}>Your Content</div>
    }
}
rehan007
  • 91
  • 1
  • 8