From 4779e1bff126ba37de0d86f17c9a18fb4768b85f Mon Sep 17 00:00:00 2001 From: Cody Lundquist Date: Mon, 3 Jun 2019 21:38:59 -0700 Subject: [PATCH] Fix an issue with setting state in the Preview component after being unmounted. --- src/Preview.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/Preview.js b/src/Preview.js index 6566bda8..061c121a 100644 --- a/src/Preview.js +++ b/src/Preview.js @@ -3,10 +3,12 @@ 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) { @@ -14,6 +16,9 @@ export default class Preview extends Component { this.fetchImage(nextProps) } } + componentWillUnmount () { + this.mounted = false + } fetchImage ({ url, light }) { if (typeof light === 'string') { this.setState({ image: light }) @@ -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 }) }