

React 17 stops event bubbling in scroll event
source link: https://blog.saeloun.com/2021/03/02/react-17-removes-bubbling-from-onscroll
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

React 17 stops event bubbling in scroll event
Mar 2, 2021 , by Chetan Gawai 2 minute readReact bubbled all its events before React 17, even the ones that the browser doesn’t bubble by default. This has caused some weird behavior #19156 #15723 with scroll event.
To demonstrate one of the issues let’s take a simple example.
A scrollable div containing some text is wrapped by parent div.
The parent div has onScroll
event attached to it which changes the background color to pink.
class App extends React.Component {
parentNode = React.createRef();
handleScroll = (e) => {
if (!ticking) {
window.requestAnimationFrame(() => {
//Change background color to pink
this.parentNode.current.style.background = '#E22C7E';
ticking = false;
});
ticking = true;
}
}
render() {
return (
<div className="wrapper">
{/* Parent div */}
<div
ref={this.parentNode}
className="parent"
onScroll={this.handleScroll}
id="parent"
>
<b>I am parent</b>
{/* Scrollable child div */}
<div className="child" id="child">
<b>I am child</b><br />
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged.
</div>
<div style= />
</div>
</div>
)
}
}
When we scroll in the child div, we can see that the background color changes to pink as shown in the below gif.
This is strange.
The reason behind this is that the scroll event of child div bubbles, calling the onScroll
event of the parent which changes the background color.
Before
With React 16 or lower, the scrolling issue could be fixed by adding a condition as below -
handleScroll = (e) => {
//Add condition
if (e.target === e.currentTarget) {
if (!ticking) {
window.requestAnimationFrame(() => {
//Change background to pink
this.parentNode.current.style.background = '#E22C7E';
ticking = false;
});
ticking = true;
}
}
}
This approach is not feasible if the hierarchy increases.
After
To avoid such issues, React 17 has stopped bubbling for a scroll event. It now aligns with the browser scroll event.
With React 17 changes, scrolling the paragraph in our example would not change the background color to pink as shown in the below gif which fixes our issue.
Check out the pull request to learn more.
Recommend
-
10
Matthew Miner Event.Use() and Scroll Views August 7, 2009 Today’s Unity discovery: calling Event.U...
-
7
Capturing vs bubbling for DOM mouse events Jul 18, 2016 As long as you take it with a grain of salt, Stack overflow is great site for finding answers to questions about programming. However, every now and then you run in...
-
10
Highlighting navigation items on scroll in React with IntersectionObserver Jan 18 ・2 min read...
-
9
react horizontal scroll CLI Commands # install dependencies npm install # serve with hot reload at localhost:3000 npm run start # Usage import HorizontalScroll from 'react-basic-horizontal-scro...
-
9
Nick Scialli • April 10, 2021 • 🚀🚀 6 minute readInfinite scrolling content is a common way to deal with large collections of data. It enables you to load only a smaller fraction of the data, save...
-
7
react-native-steve React Native horizontal scroll view component as seen on Clubhouse tags. Installati...
-
10
Event Bubbling and Capturing in JavaScriptHow to Handle JavaScript Event Propagation Life CycleJavaScript event bubbling is there to capture and handle events propagated inside the DOM. But do...
-
4
Not FoundYou just hit a route that doesn't exist... the sadness.LoginRadius empowers businesses to deliver a delightful customer experience and win customer trust. Using the LoginRadius Identity...
-
6
Elemental’s first trailer is a bubbling, sizzling feast for the eyes / In its first trailer, Pixar’s upcoming Elemental tells a familiar story with some dazzlingly gorgeous visuals.By
-
7
Event Bubbling, and Event Capturing in JavaScript Events are bound to happen all the time in a web application, or web page. Events tell the page how to behave when something happens. For insta...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK