Inspired by:
- wcandillon/react-native-expo-image-cache (William Candillon)
- WrathChaos/react-native-progressive-fast-image (FreakyCoder)
- Cache remote images in file system with progressive loading
- Uses
react-native-file-access
for file system access - Written in
Typescript
yarn add @georstat/react-native-image-cache
npm i @georstat/react-native-image-cache
import { CacheManager } from '@georstat/react-native-image-cache';
import { Dirs } from 'react-native-file-access';
CacheManager.config = {
baseDir: `${Dirs.CacheDir}/images_cache/`,
blurRadius: 15,
sourceAnimationDuration: 1000,
thumbnailAnimationDuration: 1000,
};
Directory constants, choose wisely: (react-native-file-access docs)
Dirs.CacheDir
Dirs.DatabaseDir
(Android only)Dirs.DocumentDir
Dirs.LibraryDir
(iOS only)Dirs.MainBundleDir
Dirs.SDCardDir
(Android only)- Prefer
FileSystem.cpExternal()
when possible.
- Prefer
import { CachedImage } from '@georstat/react-native-image-cache';
<CachedImage
source="https://via.placeholder.com/3500x3500"
style={{ height: 350, width: 150 }}
thumbnailSource="https://via.placeholder.com/350x150"
/>;
import { CacheManager } from '@georstat/react-native-image-cache';
await CacheManager.clearCache();
const uri = 'https://.../example.jpg';
await CacheManager.removeCacheEntry(uri);
await CacheManager.getCacheSize();
Properties | PropType | Description |
---|---|---|
source |
String |
(Required) Uri of remote image. |
sourceAnimationDuration |
Number |
source image animation duration when loading, defaults to 1000 ms (overrides config) |
thumbnailSource |
String |
(Required) Uri of the thumbnail image |
thumbnailAnimationDuration |
Number |
Animation duration for thumbnail, defaults to 1000 ms (overrides config) |
blurRadius |
Number |
Amount of blur to apply on thumbnailSource image , defaults to 15 (overrides config) |
loadingImageComponent |
React.Node |
Defaults to an image with the loadingSource prop |
loadingImageStyle |
Object |
Style for loading image component. Works if you don't provide a loadingImageComponent |
loadingSource |
object |
Source for loading Image component. Works if you don't provide loadingImageComponent |
onError |
Func |
Runs when there is an error loading the image from cache |
resizeMode |
String |
React native Image component resizeMode defaults to contain |
style |
Object |
source AND thumbnailSource image style |
options |
Object |
custom options for the fetch image http request eg. {headers:{}, body:{}} |
Convert library to React HooksMakebaseDir
configurableDelete single cache entry