Skip to content
This repository was archived by the owner on Jan 26, 2024. It is now read-only.

Commit 6de34ec

Browse files
committed
Use callback to set ref for performance
1 parent 4dbff65 commit 6de34ec

File tree

1 file changed

+14
-10
lines changed

1 file changed

+14
-10
lines changed

lib/common/Pic.jsx

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ export default class Pic extends Component {
5858
props.images[props.images.length - 1],
5959
isBlurred: props.shouldBlur,
6060
};
61-
61+
this.setPicRef = this.setPicRef.bind(this);
6262
this.setResponsiveImage = this.setResponsiveImage.bind(this);
6363
this.inViewHandler = this.inViewHandler.bind(this);
6464

@@ -81,21 +81,16 @@ export default class Pic extends Component {
8181
window.removeEventListener('resize', this.debouncedInViewHandler);
8282
}
8383

84-
/**
85-
* Sets responsive image if the element is in view
86-
*/
87-
inViewHandler() {
88-
if (this.props.renderOutOfView || isElementInView(this.refs.base)) {
89-
this.setResponsiveImage();
90-
}
84+
setPicRef(node) {
85+
this.picRef = node;
9186
}
9287

9388
/**
9489
* Set the optimal image src
9590
*/
9691
setResponsiveImage() {
9792
try {
98-
const parent = this.refs.base.parentNode;
93+
const parent = this.picRef.parentNode;
9994
const imageSlotWidth = parent.getBoundingClientRect().width;
10095

10196
const responsiveImage = getResponsiveImage(
@@ -112,13 +107,22 @@ export default class Pic extends Component {
112107
}
113108
}
114109

110+
/**
111+
* Sets responsive image if the element is in view
112+
*/
113+
inViewHandler() {
114+
if (this.props.renderOutOfView || isElementInView(this.picRef)) {
115+
this.setResponsiveImage();
116+
}
117+
}
118+
115119
render() {
116120
if (!this.state.image) {
117121
return null;
118122
}
119123

120124
return (
121-
<div ref="base" style={this.props.baseStyle} onLoad={this.inViewHandler}>
125+
<div ref={this.setPicRef} style={this.props.baseStyle} onLoad={this.inViewHandler}>
122126
<ImageWrapper {...this.props} {...this.state} />
123127
</div>
124128
);

0 commit comments

Comments
 (0)