Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React.js: Stop render from scrolling to top of page

Every time you perform a render in React.js, the UI scrolls to the top of the page.

JSFiddle: http://jsfiddle.net/bengrunfeld/dcfy5xrd/

Any nifty or reactive way to stop that?

E.g. If a User scrolls down the page, then pushes a button which causes a Render, the UI would stay in the same scroll location as before.

// Forces a render which scrolls to top of page this.setState({data: data}); 

UPDATE: Why does the UI scroll to the top for some renders, but not others?

like image 525
Ben Avatar asked May 21 '15 06:05

Ben


People also ask

How do I stop the scrollTo top in React?

2022 Hacky Solution: useEffect(() => { const el = document. getElementsByClassName('my-classname')?.[0]; const newTimeoutRef = setTimeout(() => el?. scrollTo(0, scrollTop), 50); return () => clearTimeout(newTimeoutRef); }, [scrollTop]);

How do I stop page from scrolling React?

To prevent scrolling using CSS on React rendered components, we can set the overflow CSS property to hidden with JavaScript. to set the overflow CSS of the body element to hidden when the component mounts with: document. body.

How do I change the scroll position in React?

To get scroll position with React, we can add a scroll listener to window . to create the scrollPosition state with the useState hook. Then we add the handleScroll function that takes the scroll position with the window.

How to prevent scrolling using CSS on react rendered components?

In this article, we’ll look at how to prevent scrolling using CSS on React rendered components. To prevent scrolling using CSS on React rendered components, we can set the overflow CSS property to hidden with JavaScript. The useEffect callback only runs when the component mounts since we passed in an empty array as the 2nd argument of useEffect.

How to manage scroll in react with hooks?

Create top level component to manage the scroll Now let’s create a component as ScrollToTop where we will listen to the history and set window scroll to top on componentDidMount. You can also ignore the routes for scroll to the top as well. Here we are using the React Hooks to manage the scroll position.

Why is my render trigger scrolling to the top?

If render trigger scrolling to the top it usually means some UI component is changing its dimension because of the state, this can be fixed by adding a minimum width/height Thanks for contributing an answer to Stack Overflow!

What happens to the Ui when you scroll down the page?

If a User scrolls down the page, then pushes a button which causes a Render, the UI would stay in the same scroll location as before. UPDATE: Why does the UI scroll to the top for some renders, but not others?


2 Answers

Ok if anyone read this , in my case the problem wasn't any of above. You must try first suggestions on above any way. I did everything including preventDefault() but didn't help me. The problem was ; using styled-components. Because, styled-components give a random classname every render. So it resets scroll. I gave the class name from css and solved my problem.

like image 163
escalepion Avatar answered Sep 20 '22 12:09

escalepion


@floydophone answered this one on Twitter.

https://twitter.com/floydophone/status/608129119025561600

@bengrunfeld @reactjs you forgot preventDefault() on your link handlers

like image 39
Ben Avatar answered Sep 21 '22 12:09

Ben