Cache image on react native

react native image
react-native-fast-image
react native cache image expo
react native image local file path
react native image uri local
react native image onerror
fungilation/react-native-cached-image
react-native-image-cache-wrapper

Is there a good library or maybe some default react native components that cache the image from a url? I've tried react-native-cache-image but there are a lot of issues with react-native-fs and react-native-sqlite-storage and as I am new to react native I dont know how to fix them properly.

You may be interested in my higher order component module that adds performance related image caching and "permanent cache" functionality to the native <Image> component. My module depends on react-native-fetch-blob which is the goto well-respected and battle-tested library for downloading files, so you shouldn't have dependency problems.

React Native Image Cache HOC

Tl;DR Code Example:

import imageCacheHoc from 'react-native-image-cache-hoc';
const CacheableImage = imageCacheHoc(Image);

export default class App extends Component<{}> {
  render() {
    return (
      <View style={styles.container}>
        <Text style={styles.welcome}>Welcome to React Native!</Text>
        <CacheableImage style={styles.image} source={{uri: 'https://i.redd.it/rc29s4bz61uz.png'}} />
        <CacheableImage style={styles.image} source={{uri: 'https://i.redd.it/hhhim0kc5swz.jpg'}} permanent={true} />
      </View>
  );
  }
}

The first image will be cached until the total local cache grows past 15 MB (by default) then cached images are deleted oldest first until total cache is below 15 MB again.

The second image will be stored to local disk permanently. People use this as a drop in replacement for shipping static image files with your app.

It also sounds like you are interested in arbitrarily storing image files to local disk. You can do that with a CacheableImage static method like so:

import imageCacheHoc from 'react-native-image-cache-hoc';
const CacheableImage = imageCacheHoc(Image);
CacheableImage.cacheFile('https://i.redd.it/hhhim0kc5swz.jpg', true)
  .then( localFileInfo => {
    console.log(localFileInfo);

    // Console log outputs:
    //{
    //  url: 'https://i.redd.it/rc29s4bz61uz.png',
    //  cacheType: 'permanent',
    //  localFilePath: '/this/is/absolute/path/to/file.jpg'
    //}

  });

Hope this helps!

kfiroo/react-native-cached-image: CachedImage , CachedImage is a drop in replacement for the Image component that will attempt to cache remote URLs for better performance. It's main use is to hide the cache  A better alternative, in my opinion, is a package called react-native-cached-image by Kfir Golan. React-native-cached-image provides a CachedImage component that serves as a drop-in replacement for

yarn add react-native-fast-image

Ref: https://github.com/DylanVann/react-native-fast-image

import FastImage from 'react-native-fast-image'


<FastImage
    style={{ width: 200, height: 200 }}
    source={{ uri: 'https://unsplash.it/400/400?image=1' }}
    resizeMode={FastImage.resizeMode.stretch}

/>

wcandillon/react-native-img-cache: Image Cache for React , Image Cache for React Native. Contribute to wcandillon/react-native-img-cache development by creating an account on GitHub. React Native's Image component handles image caching like browsers for the most part. If the image is returned with a Cache-Control header than that will determine the caching behaviour. e.g.

There's prefetch() method built in Image component.

Cache image on react native, You may be interested in my higher order component module that adds performance related image caching and "permanent cache"  A Complete Guide To Image & Video Caching In React Native They say that React Native is the future of the Mobile App Development. And maybe, that is justified with all the innovations & futuristic possibilities it has been offering. One such innovative feature of React Native is that you can cache the images for offline access.

Straight from the Expo docs:

import React from 'react';
import Expo from 'expo';
import { Image, View } from 'react-native';
import logo from './assets/icon.png';

const cacheImages = images => images.map(image => {
  if (typeof image === 'string') return Image.prefetch(image);
  return Expo.Asset.fromModule(image).downloadAsync();
});

class View extends React.component {
  state = {
    appIsReady: false
  }

  componentWillMount() {
    this.loadAssetsAsync();
  }

  async loadAssetsAsync = () => {
    const imageAssets = cacheImages([icon]);
    await Promise.all([...imageAssets]);
    this.setState({ appIsReady: true });
  }

  render() {
    return(
      <View>
        <Image source={logo} style={styles.imageStyle} />
      </View>
    );
  }
}

I think the code snippet is straight forward. You will have a state that will set to true when all your images are loaded. You will have the cacheImages function that will handle the work for you. The only requirement you will need is Expo.

Images · React Native, The image name is resolved the same way JS modules are resolved. <Image source={{ uri: 'https://reactjs.org/logo-og.png', cache: 'only-if-cached' }} style={{  The first image will be cached until the total local cache grows past 15 MB (by default) then cached images are deleted oldest first until total cache is below 15 MB again. The second image will be stored to local disk permanently.

I have used this library and working in both android and ios phones. It is working in Both EXPO and ReactNative. In react native automatically stored catch images.

For Installation the library:

yarn add picache

then use in your js file like this, first import the file and used it. For more information click

import Picache from "picache";

const App = () => (
     <Picache
        style={{ height: 150, width: 350 }}
        source={require("./square.png")}
      />
);

Caching Images in React Native, Learn about our RFC process, Open RFC meetings & more.Join in the discussion ! » react-native-image-cache-wrapper. 1.0.7 • Public  react-native-cached-image. CachedImage component for react-native. This package is greatly inspired by @jayesbe's amazing react-native-cacheable-image but adds some functionality that we were missing when trying to handle caching images in our react-native app. Installation npm install react-native-cached-image --save - or - yarn add react-native-cached-image

react-native-image-cache-wrapper, React Native's Image component handles image caching like browsers for the most part. If the image is returned with a Cache-Control header  React Native image cache and progressive loading for iOS and Android. Based on Expo Kit. This is a component used in the React Native Elements and the React Native Fiber starter kits. Checkout this medium story about react-native-expo-image-cache.

Image Caching in React Native, 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  The native side will then choose the best uri to display based on the measured size of the image container. A cache property can be added to control how networked request interacts with the local cache. (For more information see Cache Control for Images). The currently supported formats are png, jpg, jpeg, bmp, gif, webp (Android only), psd

How To Cache Images in an Expo Managed React Native App , One such innovative feature of React Native is that you can cache the images for offline access. This is something that React Native has  In react-native development, there are multiple caches used when the app is built: React-native packager cache; Emulator cache; Java side cache (.gradle) folder (only in android) npm cache (if relevant?) Am I missing something also? Because I'm trying to clear cache in react-native, to be able to repeat a bug that only occurs on first usage.

Comments
  • Are you try to stored cache images?
  • Can I pass JWT header token for authentication also with this in case of protected routes?
  • thanks, looks good. Probably the only one who cares about headers sended from the servers
  • Radek Czemerys, thanks, it's a good start point, but I need actually to save them to local storage (or other alternatives) with an id, and to get them on when I need.