@@ -8,36 +8,23 @@ import {defaultProps, imagePropTypes} from './imagePropTypes';
88import RESIZE_MODES from './resizeModes' ;
99
1010class Image extends React . Component {
11- constructor ( props ) {
12- super ( props ) ;
13-
14- this . debouncedConfigureImageSource = _ . debounce ( this . configureImageSource , 220 ) ;
15-
16- this . state = {
17- imageSource : undefined ,
18- } ;
19- }
20-
2111 componentDidMount ( ) {
22- this . debouncedConfigureImageSource ( ) ;
12+ this . configureOnLoad ( ) ;
2313 }
2414
2515 componentDidUpdate ( prevProps ) {
26- if ( prevProps . source . uri === this . props . source . uri ) {
16+ if ( prevProps . source === this . props . source ) {
2717 return ;
2818 }
29-
30- this . debouncedConfigureImageSource . cancel ( ) ;
31- this . debouncedConfigureImageSource ( ) ;
19+ this . configureOnLoad ( ) ;
3220 }
3321
3422 /**
3523 * Check if the image source is a URL - if so the `encryptedAuthToken` is appended
36- * to the source. The natural image dimensions can then be retrieved using this source
37- * and as a result the `onLoad` event needs to be maunually invoked to return these dimensions
24+ * to the source.
25+ * @returns { Object } - the configured image source
3826 */
39- configureImageSource ( ) {
40- this . props . onLoadStart ( ) ;
27+ getImageSource ( ) {
4128 const source = this . props . source ;
4229 let imageSource = source ;
4330 if ( this . props . isAuthTokenRequired ) {
@@ -47,25 +34,34 @@ class Image extends React.Component {
4734 const authToken = lodashGet ( this . props , 'session.encryptedAuthToken' , null ) ;
4835 imageSource = { uri : `${ source . uri } ?encryptedAuthToken=${ encodeURIComponent ( authToken ) } ` } ;
4936 }
50- this . setState ( { imageSource} ) ;
5137
38+ return imageSource ;
39+ }
40+
41+ /**
42+ * The natural image dimensions are retrieved using the updated source
43+ * and as a result the `onLoad` event needs to be manually invoked to return these dimensions
44+ */
45+ configureOnLoad ( ) {
5246 // If an onLoad callback was specified then manually call it and pass
5347 // the natural image dimensions to match the native API
5448 if ( this . props . onLoad == null ) {
5549 return ;
5650 }
5751
52+ const imageSource = this . getImageSource ( ) ;
5853 RNImage . getSize ( imageSource . uri , ( width , height ) => {
5954 this . props . onLoad ( { nativeEvent : { width, height} } ) ;
6055 } ) ;
6156 }
6257
6358 render ( ) {
64- // eslint-disable-next-line
65- const { source, onLoad, ...rest } = this . props ;
59+ // Omit the props which the underlying RNImage won't use
60+ const forwardedProps = _ . omit ( this . props , [ 'source' , 'onLoad' , 'session' , 'isAuthTokenRequired' ] ) ;
61+ const source = this . getImageSource ( ) ;
6662
67- // eslint-disable-next-line
68- return < RNImage { ...rest } source = { this . state . imageSource } /> ;
63+ // eslint-disable-next-line react/jsx-props-no-spreading
64+ return < RNImage { ...forwardedProps } source = { source } /> ;
6965 }
7066}
7167
0 commit comments