Skip to content

Commit

Permalink
Merge pull request #23 from andrew1007/hot-fix/mutation-observer-ssr
Browse files Browse the repository at this point in the history
Hot fix/mutation observer SSR
  • Loading branch information
andrew1007 authored May 27, 2022
2 parents 4405135 + 4077c57 commit e52de29
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 33 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-on-images-loaded",
"version": "2.2.7",
"version": "2.2.8",
"description": "OnImagesLoaded",
"license": "MIT",
"main": "lib/OnImagesLoaded.js",
Expand Down
64 changes: 32 additions & 32 deletions src/OnImagesLoaded.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export default class OnImagesLoaded extends Component<Props, state> {
private mounted: boolean
private _imgs: any[]
private imageLoad: HTMLDivElement | null
private observer: MutationObserver
// private observer: MutationObserver
private invokedCount: number

constructor(props: Props) {
Expand All @@ -65,7 +65,7 @@ export default class OnImagesLoaded extends Component<Props, state> {
this.mounted = false
this._imgs = []
this.imageLoad = null
this.observer = new MutationObserver(noop)
// this.observer = new MutationObserver(noop)
this.invokedCount = 0
}

Expand All @@ -79,36 +79,36 @@ export default class OnImagesLoaded extends Component<Props, state> {
componentWillUnmount() {
this.mounted = false
this._imgs.length > 0 ? this._removeImageListeners() : null
this.observer.disconnect()
}

private initObserver() {
this.observer = new MutationObserver((mutationsList) => {
for (const mutation of mutationsList) {
if (mutation.type !== 'childList') return
const { imageCount } = this.state
const nextCount = imageCount + mutation.addedNodes.length - mutation.removedNodes.length
this.setState({
imageCount: nextCount
}, () => {
mutation.removedNodes.forEach(node => {
if (node.nodeName === 'IMG') {
node.addEventListener('load', this._onUpdate)
}
})
mutation.addedNodes.forEach(node => {
if (node.nodeName === 'IMG') {
node.addEventListener('load', this._onUpdate)
}
})
})
}
})
if (this.imageLoad) {
const config = { attributes: true, childList: true, subtree: true };
this.observer.observe(this.imageLoad, config)
}
}
// this.observer.disconnect()
}

// private initObserver() {
// this.observer = new MutationObserver((mutationsList) => {
// for (const mutation of mutationsList) {
// if (mutation.type !== 'childList') return
// const { imageCount } = this.state
// const nextCount = imageCount + mutation.addedNodes.length - mutation.removedNodes.length
// this.setState({
// imageCount: nextCount
// }, () => {
// mutation.removedNodes.forEach(node => {
// if (node.nodeName === 'IMG') {
// node.addEventListener('load', this._onUpdate)
// }
// })
// mutation.addedNodes.forEach(node => {
// if (node.nodeName === 'IMG') {
// node.addEventListener('load', this._onUpdate)
// }
// })
// })
// }
// })
// if (this.imageLoad) {
// const config = { attributes: true, childList: true, subtree: true };
// this.observer.observe(this.imageLoad, config)
// }
// }

private _onUpdate() {
if (!this.mounted) return
Expand Down

0 comments on commit e52de29

Please sign in to comment.