Skip to content

Commit 5c2b4af

Browse files
authored
perf: Use React.memo for FastImage. (#449)
1 parent 89c0e2e commit 5c2b4af

File tree

1 file changed

+47
-45
lines changed

1 file changed

+47
-45
lines changed

src/index.js

+47-45
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { forwardRef } from 'react'
1+
import React, { forwardRef, memo } from 'react'
22
import PropTypes from 'prop-types'
33
import {
44
View,
@@ -11,59 +11,61 @@ import {
1111

1212
const FastImageViewNativeModule = NativeModules.FastImageView
1313

14-
const FastImage = forwardRef(
15-
(
16-
{
17-
source,
18-
onLoadStart,
19-
onProgress,
20-
onLoad,
21-
onError,
22-
onLoadEnd,
23-
style,
24-
children,
25-
fallback,
26-
...props
27-
},
28-
ref,
29-
) => {
30-
const resolvedSource = Image.resolveAssetSource(source)
31-
32-
if (fallback) {
33-
return (
34-
<View style={[styles.imageContainer, style]} ref={ref}>
35-
<Image
36-
{...props}
37-
style={StyleSheet.absoluteFill}
38-
source={resolvedSource}
39-
onLoadStart={onLoadStart}
40-
onProgress={onProgress}
41-
onLoad={onLoad}
42-
onError={onError}
43-
onLoadEnd={onLoadEnd}
44-
/>
45-
{children}
46-
</View>
47-
)
48-
}
14+
function FastImageBase({
15+
source,
16+
onLoadStart,
17+
onProgress,
18+
onLoad,
19+
onError,
20+
onLoadEnd,
21+
style,
22+
children,
23+
fallback,
24+
forwardedRef,
25+
...props
26+
}) {
27+
const resolvedSource = Image.resolveAssetSource(source)
4928

29+
if (fallback) {
5030
return (
51-
<View style={[styles.imageContainer, style]} ref={ref}>
52-
<FastImageView
31+
<View style={[styles.imageContainer, style]} ref={forwardedRef}>
32+
<Image
5333
{...props}
5434
style={StyleSheet.absoluteFill}
5535
source={resolvedSource}
56-
onFastImageLoadStart={onLoadStart}
57-
onFastImageProgress={onProgress}
58-
onFastImageLoad={onLoad}
59-
onFastImageError={onError}
60-
onFastImageLoadEnd={onLoadEnd}
36+
onLoadStart={onLoadStart}
37+
onProgress={onProgress}
38+
onLoad={onLoad}
39+
onError={onError}
40+
onLoadEnd={onLoadEnd}
6141
/>
6242
{children}
6343
</View>
6444
)
65-
},
66-
)
45+
}
46+
47+
return (
48+
<View style={[styles.imageContainer, style]} ref={forwardedRef}>
49+
<FastImageView
50+
{...props}
51+
style={StyleSheet.absoluteFill}
52+
source={resolvedSource}
53+
onFastImageLoadStart={onLoadStart}
54+
onFastImageProgress={onProgress}
55+
onFastImageLoad={onLoad}
56+
onFastImageError={onError}
57+
onFastImageLoadEnd={onLoadEnd}
58+
/>
59+
{children}
60+
</View>
61+
)
62+
}
63+
64+
const FastImageMemo = memo(FastImageBase)
65+
66+
const FastImage = forwardRef((props, ref) => (
67+
<FastImageMemo forwardedRef={ref} {...props} />
68+
))
6769

6870
FastImage.displayName = 'FastImage'
6971

0 commit comments

Comments
 (0)