Skip to content

Commit f68bb93

Browse files
committed
Clean code
1 parent dd8ce3e commit f68bb93

File tree

1 file changed

+20
-24
lines changed

1 file changed

+20
-24
lines changed

src/components/LazyLoadImage.jsx

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,14 @@ class LazyLoadImage extends React.Component {
5151
return false;
5252
}
5353

54+
componentDidUpdate(prevProps, prevState) {
55+
if (!prevState.visible && this.state.visible) {
56+
this.props.afterLoad();
57+
}
58+
59+
this.updateVisibility();
60+
}
61+
5462
getPlaceholderBoundingBox(scrollPosition = this.props.scrollPosition) {
5563
const boundingRect = this.placeholder.getBoundingClientRect();
5664
const style = ReactDOM.findDOMNode(this.placeholder).style;
@@ -67,30 +75,6 @@ class LazyLoadImage extends React.Component {
6775
};
6876
}
6977

70-
componentDidUpdate(prevProps, prevState) {
71-
if (!prevState.visible && this.state.visible) {
72-
this.props.afterLoad();
73-
}
74-
75-
this.updateVisibility();
76-
}
77-
78-
updateVisibility() {
79-
if (this.state.visible) {
80-
return;
81-
}
82-
83-
if (!this.isImageInViewport()) {
84-
return;
85-
}
86-
87-
this.props.beforeLoad();
88-
89-
this.setState({
90-
visible: true
91-
});
92-
}
93-
9478
isImageInViewport() {
9579
if (!this.placeholder) {
9680
return false;
@@ -111,6 +95,18 @@ class LazyLoadImage extends React.Component {
11195
viewport.right + threshold >= boundingBox.left);
11296
}
11397

98+
updateVisibility() {
99+
if (this.state.visible || !this.isImageInViewport()) {
100+
return;
101+
}
102+
103+
this.props.beforeLoad();
104+
105+
this.setState({
106+
visible: true
107+
});
108+
}
109+
114110
getPlaceholder() {
115111
const { className, height, placeholder, style, width } = this.props;
116112

0 commit comments

Comments
 (0)