Skip to content

Commit 2b9fb11

Browse files
committed
fix(HTMLImage): don't try to update state after the component has been unmounted
1 parent 3bff4a2 commit 2b9fb11

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

src/HTMLImage.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ export default class HTMLImage extends PureComponent {
3333

3434
componentDidMount () {
3535
this.getImageSize();
36+
this.mounted = true;
37+
}
38+
39+
componentWillUnmount () {
40+
this.mounted = false;
3641
}
3742

3843
componentWillReceiveProps (nextProps) {
@@ -75,7 +80,7 @@ export default class HTMLImage extends PureComponent {
7580
const { styleWidth, styleHeight } = this.getDimensionsFromStyle(style, height, width);
7681

7782
if (styleWidth && styleHeight) {
78-
return this.setState({
83+
return this.mounted && this.setState({
7984
width: typeof styleWidth === 'string' && styleWidth.search('%') !== -1 ? styleWidth : parseInt(styleWidth, 10),
8085
height: typeof styleHeight === 'string' && styleHeight.search('%') !== -1 ? styleHeight : parseInt(styleHeight, 10)
8186
});
@@ -85,14 +90,14 @@ export default class HTMLImage extends PureComponent {
8590
source.uri,
8691
(originalWidth, originalHeight) => {
8792
if (!imagesMaxWidth) {
88-
return this.setState({ width: originalWidth, height: originalHeight });
93+
return this.mounted && this.setState({ width: originalWidth, height: originalHeight });
8994
}
9095
const optimalWidth = imagesMaxWidth <= originalWidth ? imagesMaxWidth : originalWidth;
9196
const optimalHeight = (optimalWidth * originalHeight) / originalWidth;
92-
this.setState({ width: optimalWidth, height: optimalHeight, error: false });
97+
this.mounted && this.setState({ width: optimalWidth, height: optimalHeight, error: false });
9398
},
9499
() => {
95-
this.setState({ error: true });
100+
this.mounted && this.setState({ error: true });
96101
}
97102
);
98103
}

0 commit comments

Comments
 (0)