Skip to content

Commit

Permalink
fix(components): h5 image在unmount时取消IntersectionObserver监听 (#4975)
Browse files Browse the repository at this point in the history
  • Loading branch information
Lostsory authored and luckyadam committed Dec 2, 2019
1 parent 70ddfd8 commit a42bc6e
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions packages/taro-components/src/components/image/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@ class Image extends Nerv.Component {
aspectFillMode: 'width'
}
this.imageOnLoad = this.imageOnLoad.bind(this)
this.observer = {}
}

componentDidMount () {
if (this.props.lazyLoad) {
const lazyImg = new IntersectionObserver((entries, observer) => {
this.observer = new IntersectionObserver((entries, observer) => {
// 异步 api 关系
if (entries[entries.length - 1].isIntersecting) {
this.setState({ isLoaded: true }, () => {
Expand All @@ -28,11 +29,12 @@ class Image extends Nerv.Component {
}, {
rootMargin: '300px 0px'
})
lazyImg.observe(this.imgRef)
this.observer.observe(this.imgRef)
}
}

componentWillUnMount () {
this.observer.disconnect && this.observer.disconnect()
}

imageOnLoad (e) {
Expand Down

0 comments on commit a42bc6e

Please sign in to comment.