Skip to content

Commit 62714b0

Browse files
rubennortefacebook-github-bot
authored andcommitted
Simplify definition of Image component decorator (facebook#40730)
Summary: Pull Request resolved: facebook#40730 This way of injecting the decorator is safer and more convenient to have the proper types inferred by Flow in the injected function. Changelog: [internal] Reviewed By: NickGerleman Differential Revision: D50011840 fbshipit-source-id: 760812fc407d3e39fc7601d17488e3f2032a7065
1 parent 7345a5e commit 62714b0

File tree

3 files changed

+24
-25
lines changed

3 files changed

+24
-25
lines changed

packages/react-native/Libraries/Image/Image.android.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import flattenStyle from '../StyleSheet/flattenStyle';
1616
import StyleSheet from '../StyleSheet/StyleSheet';
1717
import TextAncestor from '../Text/TextAncestor';
1818
import ImageAnalyticsTagContext from './ImageAnalyticsTagContext';
19-
import ImageInjection from './ImageInjection';
19+
import {unstable_getImageComponentDecorator} from './ImageInjection';
2020
import {getImageSourcesFromImageProps} from './ImageSourceUtils';
2121
import {convertObjectFitToResizeMode} from './ImageUtils';
2222
import ImageViewNativeComponent from './ImageViewNativeComponent';
@@ -238,8 +238,9 @@ let BaseImage: AbstractImageAndroid = React.forwardRef(
238238
},
239239
);
240240

241-
if (ImageInjection.unstable_createImageComponent != null) {
242-
BaseImage = ImageInjection.unstable_createImageComponent(BaseImage);
241+
const imageComponentDecorator = unstable_getImageComponentDecorator();
242+
if (imageComponentDecorator != null) {
243+
BaseImage = imageComponentDecorator(BaseImage);
243244
}
244245

245246
// $FlowExpectedError[incompatible-type] Eventually we need to move these functions from statics of the component to exports in the module.

packages/react-native/Libraries/Image/Image.ios.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import {createRootTag} from '../ReactNative/RootTag';
1616
import flattenStyle from '../StyleSheet/flattenStyle';
1717
import StyleSheet from '../StyleSheet/StyleSheet';
1818
import ImageAnalyticsTagContext from './ImageAnalyticsTagContext';
19-
import ImageInjection from './ImageInjection';
19+
import {unstable_getImageComponentDecorator} from './ImageInjection';
2020
import {getImageSourcesFromImageProps} from './ImageSourceUtils';
2121
import {convertObjectFitToResizeMode} from './ImageUtils';
2222
import ImageViewNativeComponent from './ImageViewNativeComponent';
@@ -180,8 +180,9 @@ let BaseImage: AbstractImageIOS = React.forwardRef((props, forwardedRef) => {
180180
);
181181
});
182182

183-
if (ImageInjection.unstable_createImageComponent != null) {
184-
BaseImage = ImageInjection.unstable_createImageComponent(BaseImage);
183+
const imageComponentDecorator = unstable_getImageComponentDecorator();
184+
if (imageComponentDecorator != null) {
185+
BaseImage = imageComponentDecorator(BaseImage);
185186
}
186187

187188
// $FlowExpectedError[incompatible-type] Eventually we need to move these functions from statics of the component to exports in the module.

packages/react-native/Libraries/Image/ImageInjection.js

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,23 @@
44
* This source code is licensed under the MIT license found in the
55
* LICENSE file in the root directory of this source tree.
66
*
7-
* @format strict-local
8-
* @flow
7+
* @format
8+
* @flow strict-local
99
*/
1010

11-
import type {ImageProps as ImagePropsType} from './ImageProps';
11+
import type {AbstractImageAndroid, AbstractImageIOS} from './ImageTypes.flow';
1212

13-
import ImageViewNativeComponent from './ImageViewNativeComponent';
14-
import TextInlineImageNativeComponent from './TextInlineImageNativeComponent';
15-
import * as React from 'react';
13+
type ImageComponentDecorator = (AbstractImageAndroid => AbstractImageAndroid) &
14+
(AbstractImageIOS => AbstractImageIOS);
1615

17-
export default {
18-
unstable_createImageComponent: (null: ?(
19-
Image: React.AbstractComponent<
20-
ImagePropsType,
21-
| React.ElementRef<typeof TextInlineImageNativeComponent>
22-
| React.ElementRef<typeof ImageViewNativeComponent>,
23-
>,
24-
) => React.AbstractComponent<
25-
ImagePropsType,
26-
| React.ElementRef<typeof TextInlineImageNativeComponent>
27-
| React.ElementRef<typeof ImageViewNativeComponent>,
28-
>),
29-
};
16+
let injectedImageComponentDecorator: ?ImageComponentDecorator;
17+
18+
export function unstable_setImageComponentDecorator(
19+
imageComponentDecorator: ?ImageComponentDecorator,
20+
): void {
21+
injectedImageComponentDecorator = imageComponentDecorator;
22+
}
23+
24+
export function unstable_getImageComponentDecorator(): ?ImageComponentDecorator {
25+
return injectedImageComponentDecorator;
26+
}

0 commit comments

Comments
 (0)