Skip to content

Commit dd8ce3e

Browse files
committed
Avoid duplicate renders when getting new props
1 parent 75c1d55 commit dd8ce3e

File tree

3 files changed

+16
-31
lines changed

3 files changed

+16
-31
lines changed

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
"babel-plugin-transform-object-rest-spread": "^6.26.0",
1717
"babel-preset-env": "^1.6.1",
1818
"babel-preset-react": "^6.24.1",
19+
"enzyme": "^3.3.0",
20+
"enzyme-adapter-react-16": "^1.1.1",
1921
"eslint": "^4.18.1",
2022
"eslint-loader": "^1.9.0",
2123
"eslint-plugin-babel": "^4.1.2",

src/components/LazyLoadImage.jsx

Lines changed: 5 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,12 @@ class LazyLoadImage extends React.Component {
99
this.state = {
1010
visible: false
1111
};
12-
13-
this.previousBoundingBox = {
14-
bottom: -1,
15-
top: -1
16-
};
1712
}
1813

1914
componentDidMount() {
2015
this.updateVisibility();
2116
}
2217

23-
componentWillReceiveProps(nextProps) {
24-
this.updateVisibility(nextProps.scrollPosition);
25-
}
26-
2718
getRelevantProps(nextProps) {
2819
const keys = Object.keys(nextProps);
2920

@@ -81,24 +72,15 @@ class LazyLoadImage extends React.Component {
8172
this.props.afterLoad();
8273
}
8374

84-
if (this.placeholder) {
85-
const boundingBox = this.getPlaceholderBoundingBox();
86-
87-
if (this.previousBoundingBox.bottom !== boundingBox.bottom ||
88-
this.previousBoundingBox.left !== boundingBox.left ||
89-
this.previousBoundingBox.right !== boundingBox.right ||
90-
this.previousBoundingBox.top !== boundingBox.top) {
91-
this.updateVisibility();
92-
}
93-
}
75+
this.updateVisibility();
9476
}
9577

96-
updateVisibility(scrollPosition = this.props.scrollPosition) {
78+
updateVisibility() {
9779
if (this.state.visible) {
9880
return;
9981
}
10082

101-
if (!this.isImageInViewport(scrollPosition)) {
83+
if (!this.isImageInViewport()) {
10284
return;
10385
}
10486

@@ -109,12 +91,12 @@ class LazyLoadImage extends React.Component {
10991
});
11092
}
11193

112-
isImageInViewport(scrollPosition) {
94+
isImageInViewport() {
11395
if (!this.placeholder) {
11496
return false;
11597
}
11698

117-
const { threshold } = this.props;
99+
const { scrollPosition, threshold } = this.props;
118100
const boundingBox = this.getPlaceholderBoundingBox(scrollPosition);
119101
const viewport = {
120102
bottom: scrollPosition.y + window.innerHeight,
@@ -123,8 +105,6 @@ class LazyLoadImage extends React.Component {
123105
top: scrollPosition.y
124106
};
125107

126-
this.previousBoundingBox = boundingBox;
127-
128108
return Boolean(viewport.top - threshold <= boundingBox.bottom &&
129109
viewport.bottom + threshold >= boundingBox.top &&
130110
viewport.left - threshold <= boundingBox.right &&

src/components/LazyLoadImage.spec.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
import React from 'react';
22
import ReactTestUtils from 'react-dom/test-utils';
3-
import assert from 'assert';
3+
import { configure, mount } from 'enzyme';
4+
import Adapter from 'enzyme-adapter-react-16';
45

56
import LazyLoadImage from './LazyLoadImage.jsx';
67

8+
configure({ adapter: new Adapter() });
9+
710
const {
811
scryRenderedDOMComponentsWithClass,
912
scryRenderedDOMComponentsWithTag
@@ -17,7 +20,7 @@ describe('LazyLoadImage', function() {
1720
scrollPosition = {x: 0, y: 0},
1821
style = {}
1922
} = {}) {
20-
return ReactTestUtils.renderIntoDocument(
23+
return mount(
2124
<LazyLoadImage
2225
afterLoad={afterLoad}
2326
beforeLoad={beforeLoad}
@@ -40,21 +43,21 @@ describe('LazyLoadImage', function() {
4043
width: 0
4144
});
4245

43-
lazyLoadImage.placeholder.getBoundingClientRect = myMock;
46+
lazyLoadImage.instance().placeholder.getBoundingClientRect = myMock;
4447

45-
lazyLoadImage.componentWillReceiveProps({
48+
lazyLoadImage.setProps({
4649
scrollPosition: {x: offsetX, y: offsetY}
4750
});
4851
}
4952

5053
function expectImages(wrapper, numberOfImages) {
51-
const img = scryRenderedDOMComponentsWithTag(wrapper, 'img');
54+
const img = scryRenderedDOMComponentsWithTag(wrapper.instance(), 'img');
5255

5356
expect(img.length).toEqual(numberOfImages);
5457
}
5558

5659
function expectPlaceholders(wrapper, numberOfPlaceholders, placeholderClassName = 'lazy-load-image-placeholder') {
57-
const placeholder = scryRenderedDOMComponentsWithClass(wrapper, placeholderClassName);
60+
const placeholder = scryRenderedDOMComponentsWithClass(wrapper.instance(), placeholderClassName);
5861

5962
expect(placeholder.length).toEqual(numberOfPlaceholders);
6063
}

0 commit comments

Comments
 (0)