React Native Fast Image
Performant React Native image component.
[![Version][version-badge]][package]
[![Downloads][downloads-badge]][npmtrends]
[![Build Status][build-badge]][build]
[![Docs][docs-badge]][docs]
React Native's Image component handles image caching like browsers for the most part. If the server is returning proper cache control headers for images you'll generally get the sort of built in caching behavior you'd have in a browser. Even so, many people have noticed:
- Flickering.
- Cache misses.
- Low performance loading from cache.
- Low performance in general.
FastImage is an Image replacement that solves these issues. FastImage is a wrapper around SDWebImage (iOS) and Glide (Android).
Features
- Performance: in benchmarks, FastImage shows images about as fast as other React Native image components on iOS and among the fastest on Android, significantly faster in some scenarios, and uses among the least memory for large photos.
- Aggressive caching: an image downloads once, then shows from disk or memory until its url changes, without flickering (freshness, cache keys, size limits).
- Preloading, with a result per image and the cached file's path.
- Headers, e.g. for authentication (
headers).
- Priorities for which images load first (
priority).
- Responsive images: give several sizes, and the one that fits the view loads (
source).
- Sized from the image: a width or height set to
'auto' follows the loaded image's aspect ratio (style).
- Animated images (GIF, WebP, APNG and AVIF) that can loop and pause.
- Fades in and between images (
transition).
- Placeholders: a blurred preview from a ThumbHash or a tiny image while an image loads, a bundled image, or a url, e.g. a thumbnail that's already cached (
placeholder).
- SVG and photo library images.
FastImageBackground for content over an image.
- Expo and web support, with a config plugin for Expo.
Image formats
| Format |
iOS |
Android |
| JPEG |
|
|
| PNG |
|
|
| APNG |
|
|
| GIF |
|
|
| WebP |
|
|
| AVIF |
|
|
| HEIC |
|
|
| SVG |
|
|
| ICO |
|
|
| BMP |
|
|
| TIFF |
|
|
| ICNS |
|
|
| PSD |
[^psd] |
|
See Formats for the versions of iOS and Android each format needs, and what decodes it.
[^psd]: Its composite image (the layers as Photoshop saved them flattened).
Installation
npm install react-native-fast-image
cd ios && pod install
With Expo:
npx expo install react-native-fast-image
It has native code, so it needs a development build (e.g. npx expo run:ios), not Expo Go.
Needs React Native 0.83 or later with the New Architecture (Expo SDK 55 or later) and React 19, iOS/tvOS 15.1 or later, and Android 7.0 (API 24) or later; on tvOS, it works with React Native tvOS. Apps on the legacy architecture or older React Native can use FastImage 9 (npm install react-native-fast-image@9). Upgrading from FastImage 9: see Upgrading from 9.x.
To set the cache's size when the app starts, see Native config.
If your Android app has its own Glide AppGlideModule, read using FastImage with an AppGlideModule first, or FastImage may not work.
If pod install can't find a compatible SDWebImage (FastImage needs 5.21.5 or later), update it with cd ios && pod update SDWebImage.
Usage
import FastImage from 'react-native-fast-image'
const YourImage = () => (
<FastImage
style={{ width: 200, height: 200 }}
source={{
uri: 'https://example.com/image.jpg',
headers: { Authorization: 'someAuthToken' },
priority: 'normal',
}}
objectFit="contain"
/>
)
Components
FastImage
It's a single native view, which also takes React Native's View props, such as accessibility props, focus (focusable and tabIndex, and onFocus and onBlur on Android and Apple TV), testID, nativeID, onLayout, pointerEvents and hitSlop. A ref to it is that view (e.g. for measure), with reload(). It doesn't take children: use FastImageBackground for content over an image.
source
Platforms: Android, iOS, tvOS, Web · Type: number | Source | (number | Source)[]
Source for the remote image to load: a Source, a require()d image, or an array of the same image at different sizes.
When source changes, the image that's showing stays until the new one has loaded. In views that get reused for other content, such as rows in FlashList or recyclerlistview, set recyclingKey so a reused row doesn't show the previous row's image.
Several sizes of an image. source can also be an array of the same image at different sizes, each with its width and height in pixels (times scale, if it has one). The view loads the one whose size is closest to its own, in pixels, so a small view downloads a small image:
<FastImage
style={{ width: 120, height: 120 }}
source={[
{ uri: 'https://example.com/photo-200.jpg', width: 200, height: 200 },
{ uri: 'https://example.com/photo-800.jpg', width: 800, height: 800 },
]}
/>
- The view picks once it has been laid out. One that has no size (e.g. sized from
onLoad) loads the largest.
- When the view's size changes so that another size fits better, it loads that one (with its load events), and keeps showing the current image until then, without a
transition.
- Each size is cached separately. A
cacheKey applies to its own entry: give each size its own key (or none), or the sizes would replace each other in the cache.
FastImage.preload, getCachePath and writeToCache take one size: pass the one a view will show. An array fails with { ok: false, error }.
- A
require()d image can be one of the sizes: its size is the file's.
- An array of one is the same as that source.
source.uri
Platforms: Android, iOS, tvOS, Web · Type: string
Remote url to load the image from. e.g. 'https://example.com/image.jpg'.
Also loads local files (file://, and on Android content://), images in the app by name (e.g. 'my_image': in its asset catalog on iOS, a drawable on Android), photo library images on iOS and tvOS (ph://, see Photo library images) and SVG images (see SVG images).
source.headers
Platforms: Android, iOS, tvOS · Type: { [key: string]: string }
Headers to load the image with. e.g. { Authorization: 'someAuthToken' }.
source.priority
Platforms: Android, iOS, tvOS, Web · Type: Priority · Default: 'normal'
A hint for which images to start loading first when several are waiting: 'high' ones before 'normal' ones, and 'low' ones after. It's best effort, not an order: several images load at once, and when each finishes depends on its size and the network.
'low': e.g. images further down a list.
'normal': the default.
'high': e.g. the image the screen is about.
On the web, it's the image's fetchpriority.
source.cache
Platforms: Android, iOS, tvOS · Type: Cache · Default: 'immutable'
How fresh the image must be. See how caching is handled for how the options fit together.
'immutable': loads the image once, then shows the cached copy until its url (or cacheKey) changes.
'web': follows the server's HTTP cache headers, as a browser does, checking with the server when it loads. These responses are kept in their own HTTP cache (50 MB on each platform), which clearDiskCache also clears.
'cacheOnly': only shows a cached image, without making a request.
A data uri ignores it: its image is never downloaded.
source.cacheKey
Platforms: Android, iOS, tvOS · Type: string
The key the image is cached under, instead of its uri. Use it for urls that change while the image stays the same, e.g. signed urls with a token or an expiry. Use something that identifies the image, and change it when the image changes (e.g. include a version or the time it was updated), or the old image keeps showing.
Not used with cache: 'web', which follows the HTTP cache (keyed by url).
<FastImage
source={{
uri: signedUrl,
cacheKey: `avatar-${user.id}-${user.avatarUpdatedAt}`,
}}
/>
source.memoryCache
Platforms: Android, iOS, tvOS · Type: boolean · Default: true
Whether the decoded image is kept in the memory cache. With false it's only kept on disk: a view doesn't leave it in memory once it stops showing it, and FastImage.preload downloads it without decoding it. Use it for large images that are shown once or rarely, like a full-screen photo: a decoded photo can take tens of MB of memory. Images shown again, like a list scrolled back, are decoded from disk again.
source.diskCache
Platforms: Android, iOS, tvOS · Type: boolean · Default: true
Whether the image is kept on disk. With false it isn't kept in the disk cache or an HTTP cache, or read from them. Use it for images the app shouldn't keep once they've been shown (e.g. disappearing messages, or where a security policy doesn't allow caching sensitive images), images that change every time they load, or large images shown once, which would push other images out of the disk cache. The image is downloaded again whenever it isn't in memory, e.g. for a view of another size (or on Android, a change of blurRadius or objectFit), so with memoryCache: false too it's downloaded each time it's shown. With cache: 'web' it isn't kept in memory either, so it's downloaded every time it loads. With cache: 'cacheOnly' it's only looked for in memory. On Android, a large download is held in a temporary file while it loads, as every download is, deleted as soon as it's opened.
FastImage.preload, getCachePath and writeToCache resolve not ok for it: there's no file, and a view decodes the image for its own size, so it may download a preloaded one again. A source for the same url (or cacheKey) without diskCache: false still stores the image.
A data uri's image is kept on disk too, unless it's false.
source.thumbhash
Platforms: Android, iOS, tvOS, Web · Type: string
A ThumbHash of the image, shown blurred while the image loads, and if it fails to load. It's a short string you make when the image is uploaded (e.g. with the thumbhash package) and send with the image's url.
On the web, it needs import 'react-native-fast-image/thumbhash' (see ThumbHashes on the web).
<FastImage source={{ uri: photo.url, thumbhash: photo.thumbhash }} />
source.blurDataUri
Platforms: Android, iOS, tvOS, Web · Type: string
A tiny version of the image (e.g. 8 to 20 px) as a data uri, shown blurred while the image loads, and if it fails to load. On the web it shows without any JavaScript, e.g. in a server-rendered page.
<FastImage source={{ uri: photo.url, blurDataUri: photo.lqip }} />
source.width
Platforms: Android, iOS, tvOS, Web · Type: number
The image's width in points: with height, it lays out a side the style leaves out (or sets to 'auto') before the image loads (see style). With several sources (source as an array), the width of the image at this uri, from which the view picks the one for its size.
source.height
Platforms: Android, iOS, tvOS, Web · Type: number
The image's height in points (see width).
source.scale
Platforms: Android, iOS, tvOS, Web · Type: number
With several sources, the image's pixels per point at this uri: its size in pixels is width and height times scale. 1 if not given.
placeholder
Platforms: Android, iOS, tvOS, Web · Type: string | number | SourcePlaceholder | PlaceholderSource
A placeholder shown while the image loads, and if it fails to load:
- An image loaded with
require(): placeholder={require('./gray.png')}.
- A data uri:
placeholder="data:image/png;base64,…".
- The name of an image bundled with the app (an iOS asset catalog image or an Android drawable):
placeholder="gray".
- A ThumbHash, shown blurred:
placeholder={{ thumbhash }}.
- A tiny image as a data uri, shown blurred:
placeholder={{ blurDataUri }}.
- A url, loaded and cached like a source:
placeholder="https://example.com/placeholder.png", or a source with headers or a cacheKey: placeholder={{ uri, headers }}.
- A url only shown if it's already cached, e.g. a list's thumbnail while the full-size image loads:
placeholder={{ uri, cache: 'cacheOnly' }}.
A source's own thumbhash or blurDataUri is shown instead, if it has one; a url placeholder shows over it once it has loaded.
Notes:
- On Android, a
require()d image doesn't show in debug builds, where the dev server serves it. Release builds show it.
- On the web, a url loads like any image, also with
'cacheOnly': a browser can't look only in its cache.
defaultSource
Platforms: Android, iOS, tvOS, Web · Type: number
Deprecated: Use placeholder, which does the same. With both, placeholder shows.
objectFit
Platforms: Android, iOS, tvOS, Web · Type: ObjectFit · Default: 'cover'
How the image fits the view, as CSS's object-fit does.
'cover': scales it uniformly (keeping its aspect ratio) so it covers the view (inside its borders), cropping what doesn't fit.
'contain': scales it uniformly (keeping its aspect ratio) so all of it fits in the view (inside its borders).
'fill': scales its width and height separately to fill the view, which can change its aspect ratio.
'none': shows it at its own size, centered, cropped if it's larger than the view.
'scale-down': shows it at its own size, centered, or scaled down uniformly to fit if it's larger than the view (the smaller of 'none' and 'contain').
An image's own size is its size in pixels, as points (dp on Android), as CSS counts an image's pixels: a 300 × 200 image is 300 × 200 points. A bundled image (require()) is its size in points.
It can also be set in style; the prop wins.
objectPosition
Platforms: Android, iOS, tvOS, Web · Type: ObjectPosition · Default: '50% 50%'
Where the image is in the view, as CSS's object-position places it: where its edges are when it doesn't fill the view exactly (contain, cover, none and scale-down; with fill it always fills it).