1.0.0 • Published 8 years ago
@sirbrillig/ensured-image v1.0.0
EnsuredImage
A React component to reload an image if it fails.
When displaying images in a web app that needs to work offline, sometimes images on the page won't load. Using this component instead of a standard img will do two things:
- It will display a placeholder if the image fails to load.
- It will try to reload the image until it loads successfully.
Installation
Using npm:
npm i --save @sirbrillig/ensured-imageor yarn:
yarn add @sirbrillig/ensured-imageUsage
import EnsuredImage from '@sirbrillig/ensured-image'
function ImageList( { imageUrls } ) {
// (In real life you would need a `key` prop in this situation.)
return imageUrls.map( url => <EnsuredImage src={ url } /> )
}Advanced Usage
The EnsuredImage component accepts many props common to img components, including:
src(required!)classNameonClickonError(will be called after the placeholder is added.)altidsizessrcSettitle
It also includes a few special props:
PlaceholderComponent: A custom placeholder to use instead of the default.ImageComponent: A customimgcomponent to use instead of the default.setTimeout: A custom implementation of window.setTimeout.retryAfter: A number of miliseconds to wait before reloading a failed image (default: 1 minute).
1.0.0
8 years ago