Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Placeholder #2

Merged
merged 2 commits into from
Mar 15, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 58 additions & 13 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,29 @@ import {

const FastImageViewNativeModule = NativeModules.FastImageView

const FastImage = forwardRef(
(
{
class FastImage extends Component {
componentDidUpdate(prevProps, prevState) {
if (this.props.source.uri === prevProps.source.uri) {
return
}

this.setState({
loaded: false,
error: null,
})
}

state = {
loaded: false,
error: null,
}

setNativeProps(nativeProps) {
this._root.setNativeProps(nativeProps)
}

render() {
const {
source,
onLoadStart,
onProgress,
Expand All @@ -23,16 +43,22 @@ const FastImage = forwardRef(
style,
children,
fallback,
...props
},
ref,
) => {
placeholder,
forwardedRef,
...props,
} = this.props

const { loaded, error } = this.state
const resolvedSource = Image.resolveAssetSource(source)

if (fallback) {
return (
<View style={[styles.imageContainer, style]} ref={ref}>
<Image
<View
style={[styles.imageContainer, style]}
ref={forwardedRef}
>
{(!loaded || error) && placeholder}
<FastImageView
{...props}
style={StyleSheet.absoluteFill}
source={resolvedSource}
Expand All @@ -48,16 +74,29 @@ const FastImage = forwardRef(
}

return (
<View style={[styles.imageContainer, style]} ref={ref}>
<View style={[styles.imageContainer, style]} ref={forwardedRef}>
{(!loaded || error) && placeholder}
<FastImageView
{...props}
style={StyleSheet.absoluteFill}
source={resolvedSource}
onFastImageLoadStart={onLoadStart}
onFastImageProgress={onProgress}
onFastImageLoad={onLoad}
onFastImageError={onError}
onFastImageLoadEnd={onLoadEnd}
onFastImageError={data => {
this.setState({
error: true,
})

onError(data)
}}
onFastImageLoadEnd={data => {
this.setState({
loaded: true,
})

onLoadEnd(data)
}}
/>
{children}
</View>
Expand Down Expand Up @@ -104,6 +143,11 @@ FastImage.preload = sources => {

FastImage.defaultProps = {
resizeMode: FastImage.resizeMode.cover,
onLoadStart: () => {},
onProgress: () => {},
onLoad: () => {},
onError: () => {},
onLoadEnd: () => {},
}

const FastImageSourcePropType = PropTypes.shape({
Expand All @@ -122,6 +166,7 @@ FastImage.propTypes = {
onError: PropTypes.func,
onLoadEnd: PropTypes.func,
fallback: PropTypes.bool,
placeholder: PropTypes.node,
}

const FastImageView = requireNativeComponent('FastImageView', FastImage, {
Expand All @@ -134,4 +179,4 @@ const FastImageView = requireNativeComponent('FastImageView', FastImage, {
},
})

export default FastImage
export default forwardRef((props, ref) => <FastImage {...props} forwardedRef={ref} />)