Skip to content

Instantly share code, notes, and snippets.

@klinkov
Created December 19, 2016 23:03
Show Gist options
  • Save klinkov/31ec7a84de141ae1f1cc7a360daa99a3 to your computer and use it in GitHub Desktop.
Save klinkov/31ec7a84de141ae1f1cc7a360daa99a3 to your computer and use it in GitHub Desktop.
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