1
+ import _ from 'lodash'
1
2
import React , { RefObject , useCallback , useEffect , useRef } from 'react'
2
3
import { Image , ImageProps } from 'react-native'
3
4
@@ -56,10 +57,26 @@ export const ImageWithLoading = React.memo(
56
57
propsRef . current . onLoadEnd = onLoadEnd
57
58
propsRef . current . onLoadStart = onLoadStart
58
59
60
+ const callbackRef = useRef ( {
61
+ hasCalledOnError : false ,
62
+ hasCalledOnLoad : false ,
63
+ hasCalledOnLoadStart : false ,
64
+ hasCalledOnLoadEnd : false ,
65
+ } )
66
+
59
67
useEffect ( ( ) => {
60
68
updateStyles ( imageRef , { ...propsRef . current , ...stateRef . current } )
61
69
} , [ ] )
62
70
71
+ useEffect ( ( ) => {
72
+ callbackRef . current = {
73
+ hasCalledOnError : false ,
74
+ hasCalledOnLoad : false ,
75
+ hasCalledOnLoadStart : false ,
76
+ hasCalledOnLoadEnd : false ,
77
+ }
78
+ } , [ JSON . stringify ( otherProps . source || { } ) ] )
79
+
63
80
useEffect ( ( ) => {
64
81
if ( ! ( Platform . realOS === 'web' ) ) return
65
82
@@ -71,6 +88,9 @@ export const ImageWithLoading = React.memo(
71
88
} , [ imageRef . current , tooltip ] )
72
89
73
90
const handleLoad = useCallback ( e => {
91
+ if ( callbackRef . current . hasCalledOnLoad ) return
92
+ callbackRef . current . hasCalledOnLoad = true
93
+
74
94
stateRef . current . isLoading = false
75
95
stateRef . current . error = false
76
96
updateStyles ( imageRef , { ...propsRef . current , ...stateRef . current } )
@@ -80,6 +100,9 @@ export const ImageWithLoading = React.memo(
80
100
} , [ ] )
81
101
82
102
const handleLoadStart = useCallback ( ( ) => {
103
+ if ( callbackRef . current . hasCalledOnLoadStart ) return
104
+ callbackRef . current . hasCalledOnLoadStart = true
105
+
83
106
stateRef . current . isLoading = true
84
107
updateStyles ( imageRef , { ...propsRef . current , ...stateRef . current } )
85
108
@@ -88,6 +111,9 @@ export const ImageWithLoading = React.memo(
88
111
} , [ ] )
89
112
90
113
const handleLoadEnd = useCallback ( ( ) => {
114
+ if ( callbackRef . current . hasCalledOnLoadEnd ) return
115
+ callbackRef . current . hasCalledOnLoadEnd = true
116
+
91
117
stateRef . current . isLoading = false
92
118
updateStyles ( imageRef , { ...propsRef . current , ...stateRef . current } )
93
119
@@ -96,6 +122,9 @@ export const ImageWithLoading = React.memo(
96
122
} , [ ] )
97
123
98
124
const handleError = useCallback ( e => {
125
+ if ( callbackRef . current . hasCalledOnError ) return
126
+ callbackRef . current . hasCalledOnError = true
127
+
99
128
stateRef . current . isLoading = false
100
129
stateRef . current . error = true
101
130
updateStyles ( imageRef , { ...propsRef . current , ...stateRef . current } )
@@ -115,6 +144,7 @@ export const ImageWithLoading = React.memo(
115
144
/>
116
145
)
117
146
} ) ,
147
+ _ . isEqual ,
118
148
)
119
149
120
150
function updateStyles (
0 commit comments