From 3f4d1fce2ce46f08859d663d2f95bd5ebb8384c9 Mon Sep 17 00:00:00 2001 From: D N <4661784+retyui@users.noreply.github.com> Date: Fri, 12 Jun 2026 11:09:15 +0200 Subject: [PATCH] chore: Convert `ImageBackground` to function component --- .../Libraries/Image/ImageBackground.js | 111 ++++++++---------- packages/react-native/ReactNativeApi.d.ts | 17 +-- packages/react-native/index.js.flow | 5 +- 3 files changed, 63 insertions(+), 70 deletions(-) diff --git a/packages/react-native/Libraries/Image/ImageBackground.js b/packages/react-native/Libraries/Image/ImageBackground.js index 15e2243d98f2..877a481f7eac 100644 --- a/packages/react-native/Libraries/Image/ImageBackground.js +++ b/packages/react-native/Libraries/Image/ImageBackground.js @@ -8,7 +8,6 @@ * @format */ -import type {HostInstance} from '../../src/private/types/HostInstance'; import type {ImageBackgroundProps} from './ImageProps'; import View from '../Components/View/View'; @@ -53,68 +52,58 @@ export type {ImageBackgroundProps} from './ImageProps'; * @see https://reactnative.dev/docs/imagebackground * @deprecated */ -class ImageBackground extends React.Component { - setNativeProps(props: {...}) { - // Work-around flow - const viewRef = this._viewRef; - if (viewRef) { - viewRef.setNativeProps(props); - } - } - _viewRef: ?React.ElementRef = null; - - _captureRef = (ref: null | HostInstance) => { - this._viewRef = ref; - }; - - render(): React.Node { - const { - children, - style, - imageStyle, - imageRef, - importantForAccessibility, - ...props - } = this.props; - - // $FlowFixMe[underconstrained-implicit-instantiation] - const flattenedStyle = flattenStyle(style); - return ( - >, + ...props: ImageBackgroundProps +) = ({ + ref, + children, + style, + imageStyle, + imageRef, + importantForAccessibility, + ...props +}: { + ref?: React.RefSetter>, + ...ImageBackgroundProps, +}): React.Node => { + // $FlowFixMe[underconstrained-implicit-instantiation] + const flattenedStyle = flattenStyle(style); + return ( + + {/* $FlowFixMe[incompatible-use] */} + - {/* $FlowFixMe[incompatible-use] */} - overwrites width and height styles - // (which is not quite correct), and these styles conflict with explicitly set styles - // of and with our internal layout model here. - // So, we have to proxy/reapply these styles explicitly for actual component. - // This workaround should be removed after implementing proper support of - // intrinsic content size of the . - // $FlowFixMe[prop-missing] - width: flattenedStyle?.width, - // $FlowFixMe[prop-missing] - height: flattenedStyle?.height, - }, - imageStyle, - ]} - ref={imageRef} - /> - {children} - - ); - } -} + style={[ + StyleSheet.absoluteFill, + { + // Temporary Workaround: + // Current (imperfect yet) implementation of overwrites width and height styles + // (which is not quite correct), and these styles conflict with explicitly set styles + // of and with our internal layout model here. + // So, we have to proxy/reapply these styles explicitly for actual component. + // This workaround should be removed after implementing proper support of + // intrinsic content size of the . + // $FlowFixMe[prop-missing] + width: flattenedStyle?.width, + // $FlowFixMe[prop-missing] + height: flattenedStyle?.height, + }, + imageStyle, + ]} + ref={imageRef} + /> + {children} + + ); +}; -export type ImageBackgroundInstance = ImageBackground; +ImageBackground.displayName = 'ImageBackground'; export default ImageBackground; diff --git a/packages/react-native/ReactNativeApi.d.ts b/packages/react-native/ReactNativeApi.d.ts index ec87050c27de..d95aaf407c17 100644 --- a/packages/react-native/ReactNativeApi.d.ts +++ b/packages/react-native/ReactNativeApi.d.ts @@ -4,7 +4,7 @@ * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. * - * @generated SignedSource<> + * @generated SignedSource<> * * This file was generated by scripts/js-api/build-types/index.js. */ @@ -244,6 +244,12 @@ declare const I18nManager: { swapLeftAndRightInRTL: (flipStyles: boolean) => void } declare const Image: ImageType +declare const ImageBackground: typeof ImageBackground_default +declare const ImageBackground_default: ( + props: ImageBackgroundProps & { + ref?: React.Ref> + }, +) => React.ReactNode declare const InputAccessoryView: typeof InputAccessoryView_default declare const InputAccessoryView_default: React.ComponentType declare const Keyboard: typeof Keyboard_default @@ -2384,11 +2390,7 @@ declare interface ILogBox { } declare type Image = typeof Image declare type ImageAndroid = AbstractImageAndroid & ImageComponentStaticsAndroid -declare class ImageBackground extends React.Component { - render(): React.ReactNode - setNativeProps(props: {}): void -} -declare type ImageBackgroundInstance = ImageBackground +declare type ImageBackground = typeof ImageBackground declare interface ImageBackgroundProps extends Readonly< Omit > { @@ -5780,8 +5782,7 @@ export { IOSKeyboardEvent, // e67bfe3a IgnorePattern, // ec6f6ece Image, // fba54a35 - ImageBackground, // 1543bae2 - ImageBackgroundInstance, // 610d9eed + ImageBackground, // ce615745 ImageBackgroundProps, // ceb153c3 ImageErrorEvent, // 978933f4 ImageInstance, // 9a100753 diff --git a/packages/react-native/index.js.flow b/packages/react-native/index.js.flow index 51e4a54e0552..9570c4224408 100644 --- a/packages/react-native/index.js.flow +++ b/packages/react-native/index.js.flow @@ -61,7 +61,10 @@ export type { ImageURISource, } from './Libraries/Image/ImageSource'; export {default as Image} from './Libraries/Image/Image'; -export type {ImageBackgroundInstance} from './Libraries/Image/ImageBackground'; +/** + * `ImageBackgroundInstance` deprecated please use ViewInstance instead. + */ +export type {ViewInstance as ImageBackgroundInstance} from './Libraries/Components/View/View'; export {default as ImageBackground} from './Libraries/Image/ImageBackground'; export type {ImageResizeMode} from './Libraries/Image/ImageResizeMode';