@@ -30,6 +30,7 @@ function getImageSize(uri, onImageSizeSuccess, onImageSizeFailure) {
3030const initialState = {
3131 loading : true ,
3232 error : '' ,
33+ retryCount : 0 ,
3334 aspectRatio : undefined ,
3435} ;
3536
@@ -39,17 +40,24 @@ function reducer(state, action) {
3940 return {
4041 ...initialState ,
4142 loading : false ,
43+ retryCount : state . retryCount ,
4244 aspectRatio : action . payload ,
4345 } ;
4446 case 'FAILURE' :
4547 return {
4648 ...initialState ,
4749 loading : false ,
4850 error : action . payload ,
51+ retryCount : state . retryCount ,
52+ } ;
53+ case 'RETRY' :
54+ return {
55+ ...initialState ,
56+ retryCount : state . retryCount + 1 ,
4957 } ;
5058 /* istanbul ignore next: this will never happen */
5159 default :
52- return state ;
60+ throw new Error ( `Unexpected action type: ${ action . type } ` ) ;
5361 }
5462}
5563
@@ -67,6 +75,10 @@ function useResponsiveImageView({
6775
6876 const [ state , dispatch ] = React . useReducer ( reducer , initialState ) ;
6977
78+ function retry ( ) {
79+ dispatch ( { type : 'RETRY' } ) ;
80+ }
81+
7082 function isAspectRatioControlled ( ) {
7183 return controlledAspectRatio !== undefined ;
7284 }
@@ -131,15 +143,16 @@ function useResponsiveImageView({
131143 pendingGetImageSize . cancel ( ) ;
132144 } ;
133145 // Using JSON.stringify here because the `source` parameter can be a nested
134- // object. The alternative is requiring the user to memoize the parameters,
135- // but that would add usage overhead and potential confusion (at least in
136- // the early days of hooks).
146+ // object. The alternative is requiring the user to memoize it, by why make
147+ // them do that when we don't have to? (Note: they already have to memoize
148+ // onLoad and onError, but those are much less likely to be used.)
137149 // eslint-disable-next-line react-hooks/exhaustive-deps
138- } , [ JSON . stringify ( initialSource ) , onLoad , onError ] ) ;
150+ } , [ JSON . stringify ( initialSource ) , onLoad , onError , state . retryCount ] ) ;
139151
140152 return {
141153 loading : state . loading ,
142154 error : state . error ,
155+ retry,
143156 getViewProps,
144157 getImageProps,
145158 } ;
0 commit comments