Skip to content

Instantly share code, notes, and snippets.

@wcandillon
Last active February 9, 2017 14:34
Show Gist options
  • Save wcandillon/bada5bdfd74b587f3dd7633758790f0c to your computer and use it in GitHub Desktop.
Save wcandillon/bada5bdfd74b587f3dd7633758790f0c to your computer and use it in GitHub Desktop.
interface CachedImageProps {
uri: string;
style?: React.ImageStyle;
}
@observer
export class CachedImage extends Component<CachedImageProps, void> {
@observable private _path;
private handler = path => this.path = path;
@computed get path() { return this._path; }
set path(path: string) { return this._path = path; }
observe(uri: string) {
// We start observing the cache
ImageCache.on(uri, handler):
}
componentWillMount() {
observe(this.props.uri);
}
componentWillReceiveProps(nextProps: CachedImageProps) {
observe(nextProps.uri);
}
componentWillUnmount() {
// We stop observing the cache
ImageCache.dispose(handler);
}
render() {
const {style} = this.props;
return <Image style={style}
source={{ uri: this.store.path }}>{this.props.children}</Image>;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment