Skip to content

Commit 364ce21

Browse files
committed
Avoid using 'window' to store the IntersectionObserver
1 parent 72c397f commit 364ce21

File tree

1 file changed

+11
-11
lines changed

1 file changed

+11
-11
lines changed

src/components/PlaceholderWithoutTracking.jsx

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@ class PlaceholderWithoutTracking extends React.Component {
77
constructor(props) {
88
super(props);
99

10-
if (!window.LAZY_LOAD_OBSERVER) {
10+
if (!this.LAZY_LOAD_OBSERVER) {
1111
const supportsObserver = isIntersectionObserverAvailable();
1212

13-
window.LAZY_LOAD_OBSERVER = { supportsObserver };
13+
this.LAZY_LOAD_OBSERVER = { supportsObserver };
1414

1515
if (supportsObserver) {
1616
const { threshold } = props;
1717

18-
window.LAZY_LOAD_OBSERVER.observer = new IntersectionObserver(
18+
this.LAZY_LOAD_OBSERVER.observer = new IntersectionObserver(
1919
this.checkIntersections, { rootMargin: threshold + 'px' });
2020
}
2121
}
@@ -31,26 +31,26 @@ class PlaceholderWithoutTracking extends React.Component {
3131

3232
componentDidMount() {
3333
if (this.placeholder &&
34-
window.LAZY_LOAD_OBSERVER && window.LAZY_LOAD_OBSERVER.observer) {
34+
this.LAZY_LOAD_OBSERVER && this.LAZY_LOAD_OBSERVER.observer) {
3535
this.placeholder.onVisible = this.props.onVisible;
36-
window.LAZY_LOAD_OBSERVER.observer.observe(this.placeholder);
36+
this.LAZY_LOAD_OBSERVER.observer.observe(this.placeholder);
3737
}
3838

39-
if (window.LAZY_LOAD_OBSERVER &&
40-
!window.LAZY_LOAD_OBSERVER.supportsObserver) {
39+
if (this.LAZY_LOAD_OBSERVER &&
40+
!this.LAZY_LOAD_OBSERVER.supportsObserver) {
4141
this.updateVisibility();
4242
}
4343
}
4444

4545
componentWillUnMount() {
46-
if (window.LAZY_LOAD_OBSERVER) {
47-
window.LAZY_LOAD_OBSERVER.observer.unobserve(this.placeholder);
46+
if (this.LAZY_LOAD_OBSERVER) {
47+
this.LAZY_LOAD_OBSERVER.observer.unobserve(this.placeholder);
4848
}
4949
}
5050

5151
componentDidUpdate() {
52-
if (window.LAZY_LOAD_OBSERVER &&
53-
!window.LAZY_LOAD_OBSERVER.supportsObserver) {
52+
if (this.LAZY_LOAD_OBSERVER &&
53+
!this.LAZY_LOAD_OBSERVER.supportsObserver) {
5454
this.updateVisibility();
5555
}
5656
}

0 commit comments

Comments
 (0)