Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix an issue with setting state in the Preview component after being unmounted. #658

Merged
merged 1 commit into from
Jun 5, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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