Created
July 5, 2019 16:43
-
-
Save lucasfeijo/8dc8cdb52558744103cbc3bfe1c617f8 to your computer and use it in GitHub Desktop.
react-native-fast-image placeholder wrapper component
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
import React, { Component } from 'react'; | |
import { Image, View, ViewPropTypes } from 'react-native'; | |
import PropTypes from 'prop-types'; | |
import FastImage from 'react-native-fast-image'; | |
export default class FastImagePlaceholder extends Component { | |
constructor(props) { | |
super(props); | |
this.state = { loaded: false }; | |
} | |
onLoadEnd() { | |
this.setState({ loaded: true }); | |
} | |
render() { | |
return ( | |
<View> | |
{!this.state.loaded && <Image source={this.props.placeholder} style={this.props.style} />} | |
<FastImage | |
source={this.props.source} | |
style={[this.props.style, this.state.loaded ? {} : { width: 0, height: 0 }]} | |
onLoadEnd={this.onLoadEnd.bind(this)} | |
resizeMode={this.props.resizeMode} | |
/> | |
</View> | |
); | |
} | |
} | |
FastImagePlaceholder.defaultProps = { | |
resizeMode: 'contain' | |
}; | |
FastImagePlaceholder.propTypes = { | |
placeholder: PropTypes.shape({ | |
uri: PropTypes.string.isRequired | |
}).isRequired, | |
source: PropTypes.shape({ | |
uri: PropTypes.string.isRequired | |
}).isRequired, | |
style: ViewPropTypes.style, | |
resizeMode: PropTypes.string | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
it's took more performance