diff --git a/README.md b/README.md index 6502490..4982408 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# react-native-image-cache-wrapper +# react-native-image-cache-wrapper - Strange error fixed version - Алдаа зассан хувилбар [![npm](https://img.shields.io/npm/v/react-native-image-cache-wrapper.svg?style=flat-square)](https://www.npmjs.com/package/react-native-image-cache-wrapper) The best react native image cache wrapper. diff --git a/index.js b/index.js index 8779642..d05bd83 100644 --- a/index.js +++ b/index.js @@ -25,10 +25,13 @@ export default class CachedImage extends Component { static defaultProps = { expiration: 86400 * 7, // default cache a week activityIndicator: null, // default not show an activity indicator + defaultSource: require('static/images/default.png') + }; static cacheDir = RNFetchBlob.fs.dirs.CacheDir + "/CachedImage/"; + static sameURL = [] /** * delete a cache file * @param url @@ -106,6 +109,12 @@ export default class CachedImage extends Component { } const cacheFile = _getCacheFilename(url); + if(CachedImage.sameURL.includes(cacheFile)){ + + success && success(cacheFile); + return + } + CachedImage.sameURL.push(cacheFile) RNFetchBlob.fs.stat(cacheFile) .then((stats) => { @@ -118,6 +127,8 @@ export default class CachedImage extends Component { }) .catch((error) => { // not exist + // success && success(cacheFile) + _saveCacheFile(url, success, failure); }); }; @@ -160,7 +171,7 @@ export default class CachedImage extends Component { // cache failed use original source if (this._mounted) { setTimeout(() => { - this.setState({source: this.props.source}); + this.setState({source: { uri: this.props.source}}); }, 0); } this._downloading = false; @@ -190,6 +201,10 @@ export default class CachedImage extends Component { if (!this._useDefaultSource && this.props.defaultSource) { this._useDefaultSource = true; setTimeout(() => { + if(this.props.source && this.props.source.uri ){ + this.setState({source: this.props.source}); + } + else this.setState({source: this.props.defaultSource}); }, 0); } @@ -313,4 +328,4 @@ async function _saveCacheFile(url: string, success: Function, failure: Function) } catch (error) { failure && failure(error); } -} \ No newline at end of file +}