Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Window.scrollTo is undefined in React

I want to do a simple window.scrollTo(0,0) on a button click. I want it to just move the window to the top. Unfortunately the window.scrollTo(0,0) does nothing with the GUI.

I suspect it has something to do with React and the structure of the application.

scrollToTop: function() {
    window.scrollTo(0, 0);
}

window undefined

Do I have to do it in another way with react or is there something I have missed?

like image 283
magnudae Avatar asked Dec 13 '25 23:12

magnudae


1 Answers

I found that the problem was that I had a fixed position on my main container.

main-container {
   position: fixed; // Should use either static or relative
}

This made the window keep its position "fixed", which meant that window.scrollTo()would not change the position.

So be careful using position: fixed;unless you want the element to stick in the viewport. (like a header)

like image 167
magnudae Avatar answered Dec 16 '25 14:12

magnudae



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!