Languages
[Edit]
EN

React - on vertical scroll event

0 points
Created by:
Olivier-Myers
514

In this article, we would like to show you vertical onScroll event usage in React.

By default there's no onVerticalScroll method but we can achieve the same effect by monitoring the scrollTop property changes.

In the below example, we create <div> element with a scrollbar. When we scroll items vertically inside the div, onScroll triggers handleScroll method which displays messages.

Runnable example:

Note:

React documentation says: starting from React 17 the onScroll event does not bubble in React. This matches the browser behavior and prevents the confusion when a nested scrollable element fires events on a distant parent.

 

Reusable hook example

Edit

In this example, we create a reusable hook that executes the callback function only on a vertical scroll.

 

See also

Edit

Alternative titles

  1. React - onVerticalScroll scroll event
1
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join