Created
December 19, 2016 23:03
-
-
Save klinkov/31ec7a84de141ae1f1cc7a360daa99a3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class CachedImage extends Component { | |
constructor(props) { | |
super(props) | |
this.state = { | |
cachedImagePath: null | |
} | |
} | |
componentWillMount() { | |
if (this.props.source) | |
this.checkImageCache(this.props.source); | |
} | |
checkImageCache(source) { | |
let that = this; | |
return nmFetchImage(source) | |
.then(cachedResult => { | |
if (cachedResult) | |
that.setState({ | |
cachedImagePath: cachedResult | |
}) | |
}).done(); | |
} | |
render() { | |
if (this.state.cachedImagePath) { | |
return this.renderCache(); | |
} | |
if (this.props.defaultSource) { | |
return this.renderDefaultSource(); | |
} | |
return ( | |
<Spinner/> | |
) | |
} | |
renderCache() { | |
console.log(`this.props`, this.props); | |
console.log(`source`, {uri: this.state.cachedImagePath}); | |
return ( | |
<Image style={this.props.style} source={{uri: this.state.cachedImagePath}}/> | |
); | |
} | |
renderDefaultSource() { | |
const {defaultSource, ...props} = this.props; | |
return ( | |
<Image {...props} source={defaultSource}/> | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment