• Stars
    star
    729
  • Rank 59,859 (Top 2 %)
  • Language
    TypeScript
  • License
    Apache License 2.0
  • Created over 7 years ago
  • Updated almost 4 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Image Cache for React Native

React Native Image Cache

CircleCI npm version

CachedImage component and Cache image manager for React Native.

Deprecated

I am no longer maintaining this library but react-native-expo-image-cache which depends on ExpoKit.

Checkout 5 things to know about Images in React Native

Why do I need this?

Starting version 0.43, the React Native Image component has now a cache property: cache: force-cache (iOS only). This is a major improvement but only for iOS and at this time, I wasn't able to use it in a way that provides a user experience as smooth as this module.

Installation

rn-fetch-blob

This package has a dependency with rn-fetch-blob. If your project doesn't have a dependency with this package already, please refer to their installation instructions.

npm install react-native-img-cache --save

Usage

CachedImage

The CachedImage component assumes that the image URI will never change. The image is stored and served from the application cache. This component accepts the same properties than Image except for a few difference:

  • source doesn't accept an array of image URIs like Image does. Please file an issue if that's something you would like to see supported.
  • The uri property in source is mandatory.
  • The body property in source is not supported. Please file an issue if that's something you would like to see supported.
import {CachedImage} from "react-native-img-cache";

<CachedImage source={{ uri: "https://i.ytimg.com/vi/yaqe1qesQ8c/maxresdefault.jpg" }} />

The mutable property implies assumes that the image URI can change over time. The lifetime of this cache is the one of the running application and it can be manually busted using ImageCache.

import {CachedImage} from "react-native-img-cache";

<CachedImage source={{ uri: "https://i.ytimg.com/vi/yaqe1qesQ8c/maxresdefault.jpg" }} mutable />

Custom Image Component

By default, the CachedImage component is using the standard RN Image component. It is possible however to use a different component via CustomCachedImage. In the example below, we use the Image component from react-native-image-progress.

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, 
  }}/>

ImageCache

clear()

Remove cache entries and all physical files.

ImageCache.get().clear();

bust(uri)

ImageCache can be used to bust an image from the local cache. This removes the cache entry but it does not remove any physical files.

ImageCache.get().bust("https://i.ytimg.com/vi/yaqe1qesQ8c/maxresdefault.jpg");

cancel(uri)

It can also be used to cancel the download of an image. This can be very useful when scrolling through images.

ImageCache.get().cancel("https://i.ytimg.com/vi/yaqe1qesQ8c/maxresdefault.jpg");

on(uri, observer, immutable)

The ImageCache class can register observers to the cache.

const immutable = true;
const observer = (path: string) => {
    console.log(`path of the image in the cache: ${path}`);
};
ImageCache.get().on(uri, observer, immutable);

We use the observer pattern instead of a promise because a mutable image might have different version with different paths in the cache.

dispose(uri, observer)

Observers can be deregistered using dispose:

ImageCache.get().dispose(uri, observer);

Testing with jest

Mocking CachedImage

With jest, testing a snapshot can generate errors. Jest doesn't know how to generate the component CachedImage. For fix this, you have to mock CachedImage with Image component.

jest.mock('react-native-img-cache', () => {
  const mockComponent = require('react-native/jest/mockComponent')
  return {
    CustomCachedImage: mockComponent('Image'),
    CachedImage: mockComponent('Image'),
  }
})

More Repositories

1

can-it-be-done-in-react-native

βš›οΈ πŸ“Ί Projects from the β€œCan it be done in React Native?” YouTube series
TypeScript
3,974
star
2

react-native-redash

The React Native Reanimated and Gesture Handler Toolbelt
TypeScript
1,938
star
3

swagger-js-codegen

A Swagger Codegen for typescript, nodejs & angularjs
HTML
690
star
4

react-native-expo-image-cache

React Native Image Cache and Progressive Loading based on Expo
TypeScript
661
star
5

remotion-fireship

Fireship video made with React
TypeScript
333
star
6

eslint-config-react-native-wcandillon

ESLint and TypeScript configuration for React Native
JavaScript
249
star
7

react-progressive-image-loading

Progressively load images using a blur effect.
TypeScript
120
star
8

react-native-gestures-and-animations-workshop

TypeScript
117
star
9

react-native-shader

Java
107
star
10

eslint-plugin-reanimated

TypeScript
77
star
11

love-death-react

Love, Death & React
TypeScript
73
star
12

eslint-plugin-ts-exports

ESLint wrapper for ts-unused-export
TypeScript
67
star
13

jsoniq

JSONiq Implementation that compiles to JavaScript
TypeScript
63
star
14

canvaskit-js

A polyfill of CanvasKit that uses browser APIs
TypeScript
63
star
15

xqlint

JSONiq & XQuery Quality Checker
JavaScript
49
star
16

remotion-skia

TypeScript
46
star
17

react-native-static-images

Generate a file for static images for react native projects
JavaScript
21
star
18

react-native-redash-docs

19
star
19

remotion-skia-tutorial

TypeScript
18
star
20

react-skia-experiment

Ruby
16
star
21

grunt-swagger-js-codegen

Grunt Task for Swagger JS Codegen
JavaScript
16
star
22

firebase-bolt-compiler

Compiles Firebase Bolt files to TypeScript, Flow, and more
TypeScript
14
star
23

mkbhd-intro

TypeScript
11
star
24

material-ui-flow

Flow Types for Material UI
JavaScript
9
star
25

react-native-redash-v1-docs

9
star
26

expo-tutorial

TypeScript
8
star
27

react-native-image-viewer

An experiment
TypeScript
6
star
28

courrier

Postman runner that can run requests in parallel
JavaScript
6
star
29

xquery.js

Deprecated use XQLint instead
XQuery
5
star
30

language-jsoniq

JSONiq & XQuery Package for Atom
JavaScript
5
star
31

appjs-workshop-2020

5
star
32

react-native-skia

High-performance React Native Graphics using Skia
TypeScript
5
star
33

react-native-do-firebase-documentation

4
star
34

react-native-eslint-rules

My ESLint configuration for React Native Projects
JavaScript
4
star
35

react-native-do-documentation

React Native Do Documentation
4
star
36

react-native-init

3
star
37

jsoniq-mongodb-examples

JSONiq Query Examples for MongoDB
JSONiq
3
star
38

grunt-xqlint

Grunt task for XQLint
JavaScript
2
star
39

zorba.js

Zorba Compiled to JavaScript
C++
2
star
40

xqdoc

XQuery Documentation
2
star
41

react-native-do-purple-sky-documentation

React Native Do Purple Sky Documentation
2
star
42

dropbox.xq

Dropbox client library written in XQuery
XQuery
2
star
43

cloud9-xquery

XQuery extension for cloud9 IDE
JavaScript
2
star
44

cms

JSONiq
1
star
45

intro-html

A robot powered training repository πŸ€–
HTML
1
star
46

react-native-ting-documentation

React Native Ting Documentation
1
star
47

cloud9.xq

XQuery Backend for Cloud9
JavaScript
1
star
48

gist-to-png

JavaScript
1
star
49

xbrl-crawler

JavaScript
1
star
50

svg-path-properties

Pure Javascript alternative to path.getPointAtLength(t) and getTotalLength() functions. Works with Canvas & Node
TypeScript
1
star