Skip to content

Commit 363279a

Browse files
wardpeetgatsbybot
andauthored
fix(gatsby-image): check if imageRef is still available (#22255)
* fix(gatsby-image): check if imageRef is still available * Update index.js Co-authored-by: gatsbybot <mathews.kyle+gatsbybot@gmail.com>
1 parent cfc6413 commit 363279a

1 file changed

Lines changed: 7 additions & 3 deletions

File tree

packages/gatsby-image/src/index.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -395,14 +395,18 @@ class Image extends React.Component {
395395
// Once isVisible is true, imageRef becomes accessible, which imgCached needs access to.
396396
// imgLoaded and imgCached are in a 2nd setState call to be changed together,
397397
// avoiding initiating unnecessary animation frames from style changes.
398-
this.setState({ isVisible: true }, () =>
398+
this.setState({ isVisible: true }, () => {
399399
this.setState({
400400
imgLoaded: imageInCache,
401401
// `currentSrc` should be a string, but can be `undefined` in IE,
402402
// !! operator validates the value is not undefined/null/""
403-
imgCached: !!this.imageRef.current.currentSrc,
403+
// for lazyloaded components this might be null
404+
// TODO fix imgCached behaviour as it's now false when it's lazyloaded
405+
imgCached: !!(
406+
this.imageRef.current && this.imageRef.current.currentSrc
407+
),
404408
})
405-
)
409+
})
406410
})
407411
}
408412
}

0 commit comments

Comments
 (0)