Skip to content

Commit 22b695a

Browse files
committed
Use callback refs instead of string refs
1 parent 823d7a6 commit 22b695a

File tree

2 files changed

+16
-15
lines changed

2 files changed

+16
-15
lines changed

.eslintrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
"no-octal-escape": 2,
5252
"no-param-reassign": 1,
5353
"no-proto": 1,
54-
"no-return-assign": 1,
54+
"no-return-assign": 0,
5555
"no-script-url": 1,
5656
"no-self-compare": 1,
5757
"no-sequences": 1,

src/components/LazyLoadImage.jsx

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,11 @@ class LazyLoadImage extends React.Component {
6060
}
6161

6262
componentDidUpdate() {
63-
if (this.refs.placeholder) {
63+
if (this.placeholder) {
6464
const boundingBox = {
65-
bottom: this.refs.placeholder.offsetTop +
66-
this.refs.placeholder.offsetHeight,
67-
top: this.refs.placeholder.offsetTop
65+
bottom: this.placeholder.offsetTop +
66+
this.placeholder.offsetHeight,
67+
top: this.placeholder.offsetTop
6868
};
6969

7070
if (this.previousBoundingBox.bottom !== boundingBox.bottom ||
@@ -91,18 +91,18 @@ class LazyLoadImage extends React.Component {
9191
}
9292

9393
isImageInViewport(scrollPosition) {
94-
if (!this.refs.placeholder) {
94+
if (!this.placeholder) {
9595
return false;
9696
}
9797

9898
const { threshold } = this.props;
9999
const boundingBox = {
100-
bottom: this.refs.placeholder.offsetTop +
101-
this.refs.placeholder.offsetHeight,
102-
left: this.refs.placeholder.offsetLeft,
103-
right: this.refs.placeholder.offsetLeft +
104-
this.refs.placeholder.offsetWidth,
105-
top: this.refs.placeholder.offsetTop
100+
bottom: this.placeholder.offsetTop +
101+
this.placeholder.offsetHeight,
102+
left: this.placeholder.offsetLeft,
103+
right: this.placeholder.offsetLeft +
104+
this.placeholder.offsetWidth,
105+
top: this.placeholder.offsetTop
106106
};
107107
const viewport = {
108108
bottom: scrollPosition.y + window.innerHeight,
@@ -123,12 +123,13 @@ class LazyLoadImage extends React.Component {
123123
const { className, height, placeholder, width } = this.props;
124124

125125
if (placeholder) {
126-
return React.cloneElement(placeholder, { ref: 'placeholder' });
126+
return React.cloneElement(placeholder,
127+
{ ref: el => this.placeholder = el });
127128
}
128129

129130
return (
130131
<span className={'lazy-load-image-placeholder ' + className}
131-
ref="placeholder"
132+
ref={el => this.placeholder = el}
132133
style={{ height, width }}>
133134
</span>
134135
);
@@ -145,7 +146,7 @@ class LazyLoadImage extends React.Component {
145146
return (
146147
<img
147148
{...props}
148-
ref="image" />
149+
ref={img => this.image = img} />
149150
);
150151
}
151152
}

0 commit comments

Comments
 (0)