Skip to content

Commit

Permalink
Fix an issue with setting state in the Preview component after being …
Browse files Browse the repository at this point in the history
…unmounted. (#658)
  • Loading branch information
meenie authored and cookpete committed Jun 5, 2019
1 parent f1ba67a commit e37f32d
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/Preview.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,22 @@ import React, { Component } from 'react'
const ICON_SIZE = '64px'

export default class Preview extends Component {
mounted = false
state = {
image: null
}
componentDidMount () {
this.mounted = true
this.fetchImage(this.props)
}
componentWillReceiveProps (nextProps) {
if (this.props.url !== nextProps.url) {
this.fetchImage(nextProps)
}
}
componentWillUnmount () {
this.mounted = false
}
fetchImage ({ url, light }) {
if (typeof light === 'string') {
this.setState({ image: light })
Expand All @@ -23,7 +28,7 @@ export default class Preview extends Component {
return window.fetch(`https://noembed.com/embed?url=${url}`)
.then(response => response.json())
.then(data => {
if (data.thumbnail_url) {
if (data.thumbnail_url && this.mounted) {
const image = data.thumbnail_url.replace('height=100', 'height=480')
this.setState({ image })
}
Expand Down

0 comments on commit e37f32d

Please sign in to comment.