I came across an unexpected issue when adding new items to a state array in React, which consequently results in more items being added to the DOM. In Safari and Firefox, this results in the new DOM items being added below the fold, and I must scroll down to see the new items. In Chrome, the screen remains scrolled to the bottom of the page, and the items are added above. This means I need to scroll up to see the new items. This is obviously not the expected behaviour for lazy loading or a "load more" button to view more blog posts in a list.
See here: https://codesandbox.io/s/new-leaf-0fnv5?file=/src/App.js
A few other observations below:
Any suggestions on what could be influencing Chrome to behave in this unexpected way?
It seems like the solution is to use the CSS overflow-anchor: none property on the body tag (or some parent div that handles the scrolling).
https://css-tricks.com/almanac/properties/o/overflow-anchor/
Chrome's latest update done the same for me too, been searching for a couple days, new people reporting this everyday. But given the answer above, I implemented it as a hotfix for now until they admit breaking changes
For me, I had more content loading after pressing a button at the end of the content on page, and the bug was that the button was kept in view when new content was loaded so the new content was skipped, fix works however the css property can be directly on the button like so:
const useStyles = makeStyles(theme => ({
root: {
flexGrow: 1,
},
noBrowserScrolling: {
overflowAnchor: "none",
}
}));
<Button
variant="outlined"
color="default"
fullWidth
onClick={onLoadMore}
disabled={!moreExists}
className={classes.noBrowserScrolling}
>
{moreExists ? "Show More" : "No More to Load"}
</Button>
I don't have enough points to comment so have to submit a separate answer, sorry Adam
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With