Skip to content

Commit 1c51b77

Browse files
mateoguzmanafacebook-github-bot
authored andcommittedFeb 4, 2025·
Fix Android Image defaultSource runtime error (#49097)
Summary: Fixes #49075 The Image `defaultSource` prop is causing a runtime error from 0.77 just by using it in the Image component (see error in the linked issue). This might be a regression from some changes in the prop processing logic from either #47710, #47713 or #47754. ## Changelog: [ANDROID] [FIXED] - Fix Image defaultSource runtime error Pull Request resolved: #49097 Test Plan: - Verify it doesn't throw any error on runtime anymore for Android. - iOS behaviour should not be impacted as changes are Android specific In the RNTester, you can use the following component: ```tsx import * as React from 'react'; import {Image, View} from 'react-native'; function Playground() { return ( <View style={{flex: 1, justifyContent: 'center', alignItems: 'center'}}> <Image defaultSource={require('../../assets/bandaged.png')} source={{ uri: 'https://i.natgeofe.com/n/548467d8-c5f1-4551-9f58-6817a8d2c45e/NationalGeographic_2572187_4x3.jpg', }} style={{width: 200, height: 200}} /> </View> ); } ``` Also, this `defaultSource` prop is ignored in debug builds for Android ([as per the docs](https://reactnative.dev/docs/image#defaultsource)) – but I've verified we get the defaultSource as a string, which is what we expect on the native side: <details> <summary>Screenshot of the Android logs</summary> ![image](https://github.com/user-attachments/assets/e62ae2c3-6a93-4e44-a2d7-c913f5db2173) </details> Reviewed By: javache Differential Revision: D69052723 Pulled By: cortinico fbshipit-source-id: 2860dd4c18cefcfcbc4e39f94dfa6305f45773a3
1 parent bc4dee9 commit 1c51b77

File tree

4 files changed

+7
-6
lines changed

4 files changed

+7
-6
lines changed
 

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

+2
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ let BaseImage: AbstractImageAndroid = React.forwardRef(
133133
width: undefined,
134134
height: undefined,
135135
};
136+
const defaultSource = resolveAssetSource(props.defaultSource);
136137
const loadingIndicatorSource = resolveAssetSource(
137138
props.loadingIndicatorSource,
138139
);
@@ -178,6 +179,7 @@ let BaseImage: AbstractImageAndroid = React.forwardRef(
178179
/* $FlowFixMe(>=0.78.0 site=react_native_android_fb) This issue was found
179180
* when making Flow check .android.js files. */
180181
headers: (source?.[0]?.headers || source?.headers: ?{[string]: string}),
182+
defaultSource: defaultSource ? defaultSource.uri : null,
181183
loadingIndicatorSrc: loadingIndicatorSource
182184
? loadingIndicatorSource.uri
183185
: null,

‎packages/react-native/Libraries/Image/ImageViewNativeComponent.js

+3-4
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import type {
2121
} from '../StyleSheet/StyleSheet';
2222
import type {ResolvedAssetSource} from './AssetSourceResolver';
2323
import type {ImageProps} from './ImageProps';
24+
import type {ImageSource} from './ImageSource';
2425

2526
import * as NativeComponentRegistry from '../NativeComponent/NativeComponentRegistry';
2627
import {ConditionallyIgnoredEventHandlers} from '../NativeComponent/ViewConfigIgnore';
@@ -42,7 +43,7 @@ type Props = $ReadOnly<{
4243
| ?ResolvedAssetSource
4344
| ?$ReadOnlyArray<?$ReadOnly<{uri?: ?string, ...}>>,
4445
headers?: ?{[string]: string},
45-
defaultSrc?: ?string,
46+
defaultSource?: ?ImageSource | ?string,
4647
loadingIndicatorSrc?: ?string,
4748
}>;
4849

@@ -82,9 +83,7 @@ export const __INTERNAL_VIEW_CONFIG: PartialViewConfig =
8283
},
8384
validAttributes: {
8485
blurRadius: true,
85-
defaultSource: {
86-
process: require('./resolveAssetSource').default,
87-
},
86+
defaultSource: true,
8887
internal_analyticTag: true,
8988
resizeMethod: true,
9089
resizeMode: true,

‎packages/react-native/Libraries/__tests__/__snapshots__/public-api-test.js.snap

+1-1
Original file line numberDiff line numberDiff line change
@@ -4642,7 +4642,7 @@ exports[`public API should not change unintentionally Libraries/Image/ImageViewN
46424642
| ?ResolvedAssetSource
46434643
| ?$ReadOnlyArray<?$ReadOnly<{ uri?: ?string, ... }>>,
46444644
headers?: ?{ [string]: string },
4645-
defaultSrc?: ?string,
4645+
defaultSource?: ?ImageSource | ?string,
46464646
loadingIndicatorSrc?: ?string,
46474647
}>;
46484648
interface NativeCommands {

‎packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/image/ReactImageManager.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ public constructor(
124124
}
125125
}
126126

127-
@ReactProp(name = "defaultSource", customType = "ImageSource")
127+
@ReactProp(name = "defaultSource")
128128
public fun setDefaultSource(view: ReactImageView, source: String?) {
129129
view.setDefaultSource(source)
130130
}

0 commit comments

Comments
 (0)