Skip to content

Commit dfc040a

Browse files
committed
[change] Image - resolve onLoad with source
Use the same `nativeEvent` structure as in RN for the onLoad event BREAKING CHANGE `onLoad` was previously called with `nativeEvent` that was the browser Event object from the image.onload handler Since we can't spread or mutate the Event object to add `source` we have to either add it under a new key or remove it The browser Event does not expose very useful information, (no target, or size info), so it seems best to replace `nativeEvent` with the same structure used in `react-native`
1 parent a95d883 commit dfc040a

File tree

3 files changed

+14
-12
lines changed

3 files changed

+14
-12
lines changed

packages/react-native-web/src/exports/Image/index.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -320,14 +320,14 @@ const useSource = (
320320

321321
return {
322322
// Use the resolved URI for cases where it was already loaded or preloaded
323-
result: { uri },
323+
result: { source: { uri } },
324324
status: ImageLoader.has(uri) ? LOADED : IDLE
325325
};
326326
});
327327

328328
const handleLoad = React.useCallback(
329329
(result: ImageResult) => {
330-
if (onLoad) onLoad(result);
330+
if (onLoad) onLoad({ nativeEvent: result });
331331
if (onLoadEnd) onLoadEnd();
332332
setState({ status: LOADED, result });
333333
},
@@ -389,7 +389,7 @@ const useSource = (
389389

390390
return {
391391
state: state.status,
392-
loadedUri: state.result.uri
392+
loadedUri: state.result.source.uri
393393
};
394394
};
395395

packages/react-native-web/src/modules/ImageLoader/index.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -133,14 +133,15 @@ const ImageLoader = {
133133
id += 1;
134134
const image = new window.Image();
135135

136-
const handleLoad = (e) => {
136+
const handleLoad = () => {
137137
// avoid blocking the main thread
138138
const onDecode = () =>
139139
onLoad({
140-
nativeEvent: e,
141-
uri: image.src,
142-
width: image.naturalWidth,
143-
height: image.naturalHeight
140+
source: {
141+
uri: image.src,
142+
width: image.naturalWidth,
143+
height: image.naturalHeight
144+
}
144145
});
145146

146147
// Safari currently throws exceptions when decoding svgs.

packages/react-native-web/src/modules/ImageLoader/types.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@ export type ImageSource = {|
1414
|};
1515

1616
export type ImageResult = {|
17-
uri: string,
18-
width: number,
19-
height: number,
20-
nativeEvent: Event
17+
source: {
18+
uri: string,
19+
width: number,
20+
height: number
21+
}
2122
|};

0 commit comments

Comments
 (0)