-
-
Notifications
You must be signed in to change notification settings - Fork 77
Expand file tree
/
Copy pathErrorableImage.js
More file actions
30 lines (23 loc) · 736 Bytes
/
ErrorableImage.js
File metadata and controls
30 lines (23 loc) · 736 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
import React, { useState } from 'react';
import AutoHeightImage from './AutoHeightImage';
function ErrorableImage(props) {
const { source, fallbackSource, onError, ...rest } = props;
const [error, setError] = useState(false);
const shouldUseFallbackSource = error && fallbackSource;
return (
<AutoHeightImage
source={shouldUseFallbackSource ? fallbackSource : source}
onError={(_e) => {
// if an error hasn't already been seen, try to load the error image
// instead
if (!error) {
setError(true);
}
// also propagate to error handler if it is specified
onError && onError(_e);
}}
{...rest}
/>
);
}
export default ErrorableImage;