perf: Use React.memo for FastImage. (#449)
This commit is contained in:
parent
89c0e2e9ac
commit
5c2b4afa41
92
src/index.js
92
src/index.js
|
@ -1,4 +1,4 @@
|
||||||
import React, { forwardRef } from 'react'
|
import React, { forwardRef, memo } from 'react'
|
||||||
import PropTypes from 'prop-types'
|
import PropTypes from 'prop-types'
|
||||||
import {
|
import {
|
||||||
View,
|
View,
|
||||||
|
@ -11,59 +11,61 @@ import {
|
||||||
|
|
||||||
const FastImageViewNativeModule = NativeModules.FastImageView
|
const FastImageViewNativeModule = NativeModules.FastImageView
|
||||||
|
|
||||||
const FastImage = forwardRef(
|
function FastImageBase({
|
||||||
(
|
source,
|
||||||
{
|
onLoadStart,
|
||||||
source,
|
onProgress,
|
||||||
onLoadStart,
|
onLoad,
|
||||||
onProgress,
|
onError,
|
||||||
onLoad,
|
onLoadEnd,
|
||||||
onError,
|
style,
|
||||||
onLoadEnd,
|
children,
|
||||||
style,
|
fallback,
|
||||||
children,
|
forwardedRef,
|
||||||
fallback,
|
...props
|
||||||
...props
|
}) {
|
||||||
},
|
const resolvedSource = Image.resolveAssetSource(source)
|
||||||
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>
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
|
if (fallback) {
|
||||||
return (
|
return (
|
||||||
<View style={[styles.imageContainer, style]} ref={ref}>
|
<View style={[styles.imageContainer, style]} ref={forwardedRef}>
|
||||||
<FastImageView
|
<Image
|
||||||
{...props}
|
{...props}
|
||||||
style={StyleSheet.absoluteFill}
|
style={StyleSheet.absoluteFill}
|
||||||
source={resolvedSource}
|
source={resolvedSource}
|
||||||
onFastImageLoadStart={onLoadStart}
|
onLoadStart={onLoadStart}
|
||||||
onFastImageProgress={onProgress}
|
onProgress={onProgress}
|
||||||
onFastImageLoad={onLoad}
|
onLoad={onLoad}
|
||||||
onFastImageError={onError}
|
onError={onError}
|
||||||
onFastImageLoadEnd={onLoadEnd}
|
onLoadEnd={onLoadEnd}
|
||||||
/>
|
/>
|
||||||
{children}
|
{children}
|
||||||
</View>
|
</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'
|
FastImage.displayName = 'FastImage'
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue