Skip to content

Commit 21d7d10

Browse files
committed
Simplify code
1 parent 1aa87da commit 21d7d10

File tree

1 file changed

+10
-23
lines changed

1 file changed

+10
-23
lines changed

src/components/LazyLoadImage.jsx

Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -22,31 +22,14 @@ class LazyLoadImage extends React.Component {
2222
this.updateVisibility();
2323
}
2424

25-
getRelevantProps(nextProps) {
26-
const keys = Object.keys(nextProps);
27-
28-
if (!this.state.visible) {
29-
return keys;
30-
}
31-
32-
const propsToIgnoreAfterVisible = {
33-
afterLoad: true,
34-
beforeLoad: true,
35-
placeholder: true,
36-
threshold: true,
37-
scrollPosition: true,
38-
visibleByDefault: true
39-
};
40-
41-
return keys.filter(key => !propsToIgnoreAfterVisible[key]);
42-
}
43-
4425
shouldComponentUpdate(nextProps, nextState) {
4526
if (!this.state.visible) {
4627
return true;
4728
}
4829

49-
const keys = this.getRelevantProps(nextProps);
30+
const keys = this.state.visible ?
31+
Object.keys(this.getImgProps(nextProps)) :
32+
Object.keys(nextProps);
5033

5134
for (let i = 0; i < keys.length; i++) {
5235
const key = keys[i];
@@ -131,12 +114,16 @@ class LazyLoadImage extends React.Component {
131114
);
132115
}
133116

134-
render() {
117+
getImgProps(props) {
135118
const { afterLoad, beforeLoad, placeholder, scrollPosition, threshold,
136-
visibleByDefault, ...props } = this.props;
119+
visibleByDefault, ...imgProps } = props;
137120

121+
return imgProps;
122+
}
123+
124+
render() {
138125
return this.state.visible ?
139-
<img {...props} /> :
126+
<img {...this.getImgProps(this.props)} /> :
140127
this.getPlaceholder();
141128
}
142129
}

0 commit comments

Comments
 (0)