Awesome React Native Image Caching Libraries

By | August 4, 2018

Image Loading and caching had been a little bit pain in react native.
In-fact react native didn’t had native support for caching.

But after the recent release, there is now support for caching in iOS only. Still not for Android.

You can read more about this from here
https://facebook.github.io/react-native/docs/images.html#cache-control-ios-only

[html]
<Image
source={{
uri: ‘https://facebook.github.io/react/logo-og.png’,
cache: ‘only-if-cached’,
}}
style={{width: 400, height: 400}}
/>
[/html]

Now these are the variety of options available.

default: Use the native platforms default strategy.
reload: The data for the URL will be loaded from the originating source. No existing cache data should be used to satisfy a URL load request.
force-cache: The existing cached data will be used to satisfy the request, regardless of its age or expiration date. If there is no existing data in the cache corresponding the request, the data is loaded from the originating source.
only-if-cached: The existing cache data will be used to satisfy a request, regardless of its age or expiration date. If there is no existing data in the cache corresponding to a URL load request, no attempt is made to load the data from the originating source, and the load is considered to have failed.

Libraries

Below are some third party libraries that does the same job, but it has support for both android and iOS.

1. React-native-cached-image

Example Source Code

[html]
import React from ‘react’;
import {
CachedImage,
ImageCacheProvider
} from ‘react-native-cached-image’;

const images = [
‘https://example.com/images/1.jpg’,
‘https://example.com/images/2.jpg’,
‘https://example.com/images/3.jpg’,
// …
];

export default class Example extends React.Component {
render() {
return (
<ImageCacheProvider
urlsToPreload={images}
onPreloadComplete={() => console.log(‘hey there’)}>

<CachedImage source={{uri: images[0]}}/>

<CachedImage source={{uri: images[1]}}/>

<CachedImage source={{uri: images[2]}}/>

</ImageCacheProvider>
);
}
}
[/html]

2. React Native Image Cache

[html]
import {CustomCachedImage} from "react-native-img-cache";

import Image from ‘react-native-image-progress’;
import ProgressBar from ‘react-native-progress/Bar’;

<CustomCachedImage
component={Image}
source={{ uri: ‘http://loremflickr.com/640/480/dog’ }}
indicator={ProgressBar}
style={{
width: 320,
height: 240,
}}/>

[/html]

You can explore more options from the respective pages.

Leave a Reply

Your email address will not be published. Required fields are marked *