How To Cache Images – React Native Expo (Managed)
By Lane Wagner – @wagslane on Twitter
Caching images in React Native can be easy, even if you are using Expo’s managed workflow. The problem many devs run into is that React Native only supports caching images on IOS out of the box.
Other popular community packages that work on Android contain native code, and as such don’t work with Expo’s managed workflow. For this reason, I open-sourced the code I’m using on my latest project. Behold, react-native-expo-cached-image!
Quick Start
Install the module:
yarn add react-native-expo-cached-image
Import the component:
import CachedImage from 'react-native-expo-cached-image';
Use the component in a render() method:
<CachedImage
isBackground
source=
/>
The CachedImage component has the same props and API as React Native’s Image and ImageBackground components. To use CachedImage as a background image, just pass in the isBackground prop:
<CachedImage
isBackground
source=
/>
What Is It Doing?
CachedImage keeps it simple. It downloads the image to the user’s local filesystem using the SHA-256 hash of the URI. Then, on subsequent renders and app uses, it loads the image from the filesystem if it exists. This saves the user from using unnecessary data and experiencing slow load times.
Tip: In order to bust the cache, you can append a query string or anchor text to the URI.
Code
As of writing, here is the code, feel free to just copypasta it if you don’t want to install the dependency:
import React, { Component } from 'react';
import { View, Image, ImageBackground } from 'react-native';
import * as FileSystem from 'expo-file-system';
import * as Crypto from 'expo-crypto';
export default class CachedImage extends Component {
state = {
imgURI: ''
}
async componentDidMount() {
const filesystemURI = await this.getImageFilesystemKey(this.props.source.uri);
await this.loadImage(filesystemURI, this.props.source.uri);
}
async componentDidUpdate() {
const filesystemURI = await this.getImageFilesystemKey(this.props.source.uri);
if (this.props.source.uri === this.state.imgURI ||
filesystemURI === this.state.imgURI) {
return null;
}
await this.loadImage(filesystemURI, this.props.source.uri);
}
async getImageFilesystemKey(remoteURI) {
const hashed = await Crypto.digestStringAsync(
Crypto.CryptoDigestAlgorithm.SHA256,
remoteURI
);
return `${FileSystem.cacheDirectory}${hashed}`;
}
async loadImage(filesystemURI, remoteURI) {
try {
// Use the cached image if it exists
const metadata = await FileSystem.getInfoAsync(filesystemURI);
if (metadata.exists) {
this.setState({
imgURI: filesystemURI
});
return;
}
// otherwise download to cache
const imageObject = await FileSystem.downloadAsync(
remoteURI,
filesystemURI
);
this.setState({
imgURI: imageObject.uri
});
}
catch (err) {
console.log('Image loading error:', err);
this.setState({ imgURI: remoteURI });
}
}
render() {
return (
<View>
{this.props.isBackground ? (
<ImageBackground
{...this.props}
source={this.state.imgURI ? { uri: this.state.imgURI } : null}
>
{this.props.children}
</ImageBackground>
) : (
<Image
{...this.props}
source={this.state.imgURI ? { uri: this.state.imgURI } : null}
/>
)}
</View>
);
}
}
Thanks For Reading
Lane on Twitter: @wagslane
Lane on Dev.to: wagslane
Download Qvault: https://qvault.io
The post How To Cache Images – React Native Expo (Managed) appeared first on Qvault.
source https://qvault.io/2020/02/04/how-to-cache-images-react-native-expo-managed/?utm_source=rss&utm_medium=rss&utm_campaign=how-to-cache-images-react-native-expo-managed
Comments
Post a Comment