You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Apr 15, 2025. It is now read-only.
Copy file name to clipboardExpand all lines: README.md
+92-2Lines changed: 92 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -39,7 +39,11 @@ To troubleshoot linking, refer to [the react-native-fetch-blob installation inst
39
39
40
40
React Native Image Cache HOC creates an advanced image component, \<CacheableImage\>, that is a drop in replacement for the standard \<Image\> component.
41
41
42
-
The only change in the advanced component API is the component "source" prop only accepts a web accessible url (there's no reason to use this library to render files that already exist on the local filesystem). Additionally there is a new, optional, prop "permanent" that determines if the image file should be stored forever on the local filesystem instead of written to a temperary cache. Typically "permanent" images would be static files that would traditionally ship with with app itself.
42
+
**Differences between the advanced image component and standard image component API are as follows:**
43
+
44
+
1.**Modified "source" Prop:** The advanced component "source" prop only accepts a web accessible url (there's no reason to use this library to render files that already exist on the local filesystem), and it does NOT accept an array of urls.
45
+
2.**New "permanent" Prop:** The new, optional (defaults to False), "permanent" prop determines if the image file should be stored forever on the local filesystem instead of written to a temperary cache that is subject to occasional pruning.
46
+
3.**New "placeholder" Prop:** The new, optional (defaults to standard Image component), "placeholder" prop determines component to render while remote image file is downloading.
43
47
44
48
**TL;DR: To cache image files for performance, simply use \<CacheableImage\> as a drop in replacement for \<Image\>. To store files permanently add a permanent={true} prop to \<CacheableImage\>.**
45
49
@@ -119,11 +123,97 @@ imageCacheHoc(Image, {
119
123
// but sequential writes to the cache will trigger cache pruning
120
124
// which will delete cached files until total cache size is below this limit before writing.
121
125
// Defaults to 15 MB.
122
-
cachePruneTriggerLimit:1024*1024*10
126
+
cachePruneTriggerLimit:1024*1024*10,
127
+
128
+
// Default placeholder component to render while remote image file is downloading.
129
+
// Can be overridden with placeholder prop like <CacheableImage placeholder={placeHolderObject} />.
// Defaults to <Image> component with style prop passed through.
141
+
defaultPlaceholder: {
142
+
component: ActivityIndicator,
143
+
props: {
144
+
style: activityIndicatorStyle
145
+
}
146
+
}
123
147
124
148
});
125
149
```
126
150
151
+
## Using Loading Placeholders
152
+
153
+
React Native Image Cache HOC allows you to easily supply any component to be used as a placeholder while the remote image file is downloading. While the default placeholder should be great for many use cases, you can easily use your own to match the style of the rest of your app.
154
+
155
+
```js
156
+
157
+
conststyles=StyleSheet.create({
158
+
container: {
159
+
flex:1,
160
+
justifyContent:'center',
161
+
alignItems:'center',
162
+
backgroundColor:'#F5FCFF',
163
+
},
164
+
welcome: {
165
+
fontSize:20,
166
+
textAlign:'center',
167
+
margin:10,
168
+
},
169
+
image: {
170
+
width:150,
171
+
height:204
172
+
},
173
+
activityIndicatorStyle: {
174
+
width:150,
175
+
height:204,
176
+
backgroundColor:'#dc143c'
177
+
}
178
+
});
179
+
180
+
// This placeholder object will be used as a placeholder component for all instances of <CacheableImage>
181
+
// unless individual <CacheableImage> uses "placeholder" prop to override this default.
182
+
constdefaultPlaceholderObject= {
183
+
component: ActivityIndicator,
184
+
props: {
185
+
style:styles.activityIndicatorStyle
186
+
}
187
+
};
188
+
189
+
// We will use this placeholder object to override the default placeholder.
190
+
constpropOverridePlaceholderObject= {
191
+
component: Image,
192
+
props: {
193
+
style:styles.image,
194
+
source: {require('./localPlaceholderImage.png')}
195
+
}
196
+
};
197
+
198
+
constCacheableImage=imageCacheHoc(Image, {
199
+
defaultPlaceholder: defaultPlaceholderObject
200
+
});
201
+
202
+
exportdefaultclassAppextendsComponent<{}> {
203
+
render() {
204
+
return (
205
+
<View style={styles.container}>
206
+
<Text style={styles.welcome}>Welcome to React Native!</Text>
React Native Image Cache HOC must be run in a native environment to work correctly. As a result it will create issues in your jest tests unless you mock it. Since this module is an HOC that adds additional functionality to the standard \<Image\> component, it can be easily mocked with a function that returns the standard \<Image\> component.
if(options.validProtocols&&!Array.isArray(options.validProtocols)){thrownewError('validProtocols option must be an array of protocol strings.');}
29
+
if(options.fileHostWhitelist&&!Array.isArray(options.fileHostWhitelist)){thrownewError('fileHostWhitelist option must be an array of host strings.');}
30
+
if(options.cachePruneTriggerLimit&&!Number.isInteger(options.cachePruneTriggerLimit)){thrownewError('cachePruneTriggerLimit option must be an integer.');}
31
+
if(options.fileDirName&&typeofoptions.fileDirName!=='string'){thrownewError('fileDirName option must be string');}
32
+
if(options.defaultPlaceholder&&(!options.defaultPlaceholder.component||!options.defaultPlaceholder.props)){thrownewError('defaultPlaceholder option object must include "component" and "props" properties (props can be an empty object)');}
cachePruneTriggerLimit: options.cachePruneTriggerLimit||1024*1024*15,// Maximum size of image file cache in bytes before pruning occurs. Defaults to 15 MB.
52
-
fileDirName: options.fileDirName||null// Namespace local file writing to this directory. Defaults to 'react-native-image-cache-hoc'.
59
+
fileDirName: options.fileDirName||null,// Namespace local file writing to this directory. Defaults to 'react-native-image-cache-hoc'.
60
+
defaultPlaceholder: options.defaultPlaceholder||null,// Default placeholder component to render while remote image file is downloading. Can be overridden with placeholder prop. Defaults to <Image> component with style prop passed through.
0 commit comments