forked from vivaxy/react-native-auto-height-image
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathautoHeightImageWithErrorFallback.js
More file actions
36 lines (30 loc) · 964 Bytes
/
autoHeightImageWithErrorFallback.js
File metadata and controls
36 lines (30 loc) · 964 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import React, { Component } from 'react';
import AutoHeightImage from './autoHeightImage';
export default class ErrorableImage extends Component {
static propTypes = {
...AutoHeightImage.propTypes,
fallbackSource: AutoHeightImage.propTypes.source
};
state = { error: false };
render() {
const { source, fallbackSource, onError, ...restProps } = this.props;
const shouldUseFallbackSource = this.state.error && fallbackSource;
return (
<AutoHeightImage
source={shouldUseFallbackSource ? fallbackSource : source}
onError={(error) => {
// if an error hasn't already been seen, try to load the error image
// instead
if (!this.state.error) {
this.setState({ error: true });
}
// also propagate to error handler if it is specified
if (onError) {
onError(error);
}
}}
{...restProps}
/>
);
}
}