Skip to content

Commit 682cc69

Browse files
committed
[Android] Fix performance + Fix blinking images
Caused by: facebook/react-native#25201 Closes pmndrs/react-spring#570 Related: #126
1 parent 4b4eb26 commit 682cc69

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

packages/components/src/components/common/ImageWithLoading.tsx

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import _ from 'lodash'
12
import React, { RefObject, useCallback, useEffect, useRef } from 'react'
23
import { Image, ImageProps } from 'react-native'
34

@@ -56,10 +57,26 @@ export const ImageWithLoading = React.memo(
5657
propsRef.current.onLoadEnd = onLoadEnd
5758
propsRef.current.onLoadStart = onLoadStart
5859

60+
const callbackRef = useRef({
61+
hasCalledOnError: false,
62+
hasCalledOnLoad: false,
63+
hasCalledOnLoadStart: false,
64+
hasCalledOnLoadEnd: false,
65+
})
66+
5967
useEffect(() => {
6068
updateStyles(imageRef, { ...propsRef.current, ...stateRef.current })
6169
}, [])
6270

71+
useEffect(() => {
72+
callbackRef.current = {
73+
hasCalledOnError: false,
74+
hasCalledOnLoad: false,
75+
hasCalledOnLoadStart: false,
76+
hasCalledOnLoadEnd: false,
77+
}
78+
}, [JSON.stringify(otherProps.source || {})])
79+
6380
useEffect(() => {
6481
if (!(Platform.realOS === 'web')) return
6582

@@ -71,6 +88,9 @@ export const ImageWithLoading = React.memo(
7188
}, [imageRef.current, tooltip])
7289

7390
const handleLoad = useCallback(e => {
91+
if (callbackRef.current.hasCalledOnLoad) return
92+
callbackRef.current.hasCalledOnLoad = true
93+
7494
stateRef.current.isLoading = false
7595
stateRef.current.error = false
7696
updateStyles(imageRef, { ...propsRef.current, ...stateRef.current })
@@ -80,6 +100,9 @@ export const ImageWithLoading = React.memo(
80100
}, [])
81101

82102
const handleLoadStart = useCallback(() => {
103+
if (callbackRef.current.hasCalledOnLoadStart) return
104+
callbackRef.current.hasCalledOnLoadStart = true
105+
83106
stateRef.current.isLoading = true
84107
updateStyles(imageRef, { ...propsRef.current, ...stateRef.current })
85108

@@ -88,6 +111,9 @@ export const ImageWithLoading = React.memo(
88111
}, [])
89112

90113
const handleLoadEnd = useCallback(() => {
114+
if (callbackRef.current.hasCalledOnLoadEnd) return
115+
callbackRef.current.hasCalledOnLoadEnd = true
116+
91117
stateRef.current.isLoading = false
92118
updateStyles(imageRef, { ...propsRef.current, ...stateRef.current })
93119

@@ -96,6 +122,9 @@ export const ImageWithLoading = React.memo(
96122
}, [])
97123

98124
const handleError = useCallback(e => {
125+
if (callbackRef.current.hasCalledOnError) return
126+
callbackRef.current.hasCalledOnError = true
127+
99128
stateRef.current.isLoading = false
100129
stateRef.current.error = true
101130
updateStyles(imageRef, { ...propsRef.current, ...stateRef.current })
@@ -115,6 +144,7 @@ export const ImageWithLoading = React.memo(
115144
/>
116145
)
117146
}),
147+
_.isEqual,
118148
)
119149

120150
function updateStyles(

0 commit comments

Comments
 (0)