Last active
February 9, 2017 14:34
-
-
Save wcandillon/bada5bdfd74b587f3dd7633758790f0c to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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