Skip to content

Commit

Permalink
perf: Use React.memo for FastImage. (DylanVann#449)
Browse files Browse the repository at this point in the history
  • Loading branch information
DylanVann authored Apr 21, 2019
1 parent 89c0e2e commit 5c2b4af
Showing 1 changed file with 47 additions and 45 deletions.
92 changes: 47 additions & 45 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { forwardRef } from 'react'
import React, { forwardRef, memo } from 'react'
import PropTypes from 'prop-types'
import {
View,
Expand All @@ -11,59 +11,61 @@ import {

const FastImageViewNativeModule = NativeModules.FastImageView

const FastImage = forwardRef(
(
{
source,
onLoadStart,
onProgress,
onLoad,
onError,
onLoadEnd,
style,
children,
fallback,
...props
},
ref,
) => {
const resolvedSource = Image.resolveAssetSource(source)

if (fallback) {
return (
<View style={[styles.imageContainer, style]} ref={ref}>
<Image
{...props}
style={StyleSheet.absoluteFill}
source={resolvedSource}
onLoadStart={onLoadStart}
onProgress={onProgress}
onLoad={onLoad}
onError={onError}
onLoadEnd={onLoadEnd}
/>
{children}
</View>
)
}
function FastImageBase({
source,
onLoadStart,
onProgress,
onLoad,
onError,
onLoadEnd,
style,
children,
fallback,
forwardedRef,
...props
}) {
const resolvedSource = Image.resolveAssetSource(source)

if (fallback) {
return (
<View style={[styles.imageContainer, style]} ref={ref}>
<FastImageView
<View style={[styles.imageContainer, style]} ref={forwardedRef}>
<Image
{...props}
style={StyleSheet.absoluteFill}
source={resolvedSource}
onFastImageLoadStart={onLoadStart}
onFastImageProgress={onProgress}
onFastImageLoad={onLoad}
onFastImageError={onError}
onFastImageLoadEnd={onLoadEnd}
onLoadStart={onLoadStart}
onProgress={onProgress}
onLoad={onLoad}
onError={onError}
onLoadEnd={onLoadEnd}
/>
{children}
</View>
)
},
)
}

return (
<View style={[styles.imageContainer, style]} ref={forwardedRef}>
<FastImageView
{...props}
style={StyleSheet.absoluteFill}
source={resolvedSource}
onFastImageLoadStart={onLoadStart}
onFastImageProgress={onProgress}
onFastImageLoad={onLoad}
onFastImageError={onError}
onFastImageLoadEnd={onLoadEnd}
/>
{children}
</View>
)
}

const FastImageMemo = memo(FastImageBase)

const FastImage = forwardRef((props, ref) => (
<FastImageMemo forwardedRef={ref} {...props} />
))

FastImage.displayName = 'FastImage'

Expand Down

0 comments on commit 5c2b4af

Please sign in to comment.