Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions config/webpack/webpack.common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ const includeModules = [
'expo',
'expo-audio',
'expo-video',
'expo-image',
'expo-image-manipulator',
'expo-modules-core',
'victory-native',
Expand Down
14 changes: 1 addition & 13 deletions patches/react-native-web/details.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,6 @@
- E/App issue: 🛑
- PR introducing patch: https://github.com/Expensify/App/pull/32843

### [react-native-web+0.21.2+003+image-header-support.patch](react-native-web+0.21.2+003+image-header-support.patch)

- Reason:

```
Adds support for Image component with HTTP headers.
```

- Upstream PR/issue: https://github.com/necolas/react-native-web/pull/2442
- E/App issue: 🛑
- PR introducing patch: https://github.com/Expensify/App/pull/13036

### [react-native-web+0.21.2+004+fixPointerEventDown.patch](react-native-web+0.21.2+004+fixPointerEventDown.patch)

- Reason:
Expand Down Expand Up @@ -145,4 +133,4 @@
```
- Upstream PR/issue: https://github.com/necolas/react-native-web/issues/2817
- E/App issue: https://github.com/Expensify/App/issues/73782
- PR introducing patch: https://github.com/Expensify/App/pull/76332
- PR introducing patch: https://github.com/Expensify/App/pull/76332

This file was deleted.

26 changes: 17 additions & 9 deletions src/components/Image/BaseImage.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import {Image as ExpoImage} from 'expo-image';
import type {ImageLoadEventData} from 'expo-image';
import React, {useCallback, useContext, useEffect} from 'react';
import {Image as RNImage} from 'react-native';
import type {ImageLoadEvent, ImageSourcePropType} from 'react-native';
import type {AttachmentSource} from '@components/Attachments/types';
import getImageRecyclingKey from '@libs/getImageRecyclingKey';
import {AttachmentStateContext} from '@pages/media/AttachmentModalScreen/AttachmentModalBaseContent/AttachmentStateContextProvider';
import type {BaseImageProps} from './types';

function BaseImage({onLoad, source, ...props}: BaseImageProps) {
function BaseImage({onLoad, onLoadStart, source, ...props}: BaseImageProps) {
const {setAttachmentLoaded, isAttachmentLoaded} = useContext(AttachmentStateContext);
useEffect(() => {
if (isAttachmentLoaded?.(source as AttachmentSource)) {
Expand All @@ -16,27 +16,35 @@ function BaseImage({onLoad, source, ...props}: BaseImageProps) {
setAttachmentLoaded?.(source as AttachmentSource, false);
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);

useEffect(() => {
if (!source) {
return;
}
// expo-image doesn't support onLoadStart on web, so we call it manually when the source changes, matching react-native-web's Image behavior
onLoadStart?.();
}, [source, onLoadStart]);
Comment on lines +20 to +26

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Stop retriggering onLoadStart on every render

Calling onLoadStart from a useEffect that depends on both source and onLoadStart causes it to fire on every rerender when callers pass non-memoized handlers (for example ImageView passes onLoadStart={imageLoadingStart} in src/components/ImageView/index.tsx, where imageLoadingStart is recreated each render). On web this can repeatedly enqueue state updates (setImageSize({width: 0, height: 0})) and lead to render thrashing or an update loop even when the image URL has not changed, which is a behavior regression from the prior event-driven react-native image implementation.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that previously the same kind of logic was used in the Image from react-native-web, it also called onLoadStart from a useEffect that depends on both source and onLoadStart:
https://github.com/necolas/react-native-web/blob/a9de220ba9e65bdea540fb5322ffb1da2b0bf442/packages/react-native-web/src/exports/Image/index.js#L278-L322

Just maybe I need to add a check for the source to be not a falsy value.


const imageLoadedSuccessfully = useCallback(
(event: ImageLoadEvent) => {
(event: ImageLoadEventData) => {
setAttachmentLoaded?.(source as AttachmentSource, true);
if (!onLoad) {
return;
}

// We override `onLoad`, so both web and native have the same signature
const {width, height} = event.nativeEvent.source;
const {width, height} = event.source;
onLoad({nativeEvent: {width, height}});
},
[onLoad, source, setAttachmentLoaded],
);

return (
<RNImage
<ExpoImage
// Only subscribe to onLoad when a handler is provided to avoid unnecessary event registrations, optimizing performance.
onLoad={onLoad ? imageLoadedSuccessfully : undefined}
source={source as ImageSourcePropType}
// TODO: Replace with recyclingKey when the component is migrated to expo-image
key={getImageRecyclingKey(source)}
source={source}
recyclingKey={getImageRecyclingKey(source)}
// eslint-disable-next-line react/jsx-props-no-spreading
{...props}
/>
Expand Down
3 changes: 3 additions & 0 deletions src/components/Image/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ type BaseImageProps = {
/** Event for when the image is fully loaded and returns the natural dimensions of the image */
onLoad?: (event: ImageOnLoadEvent) => void;

/** Called when the image starts to load. */
onLoadStart?: () => void;

/** Styles for the Image */
style?: StyleProp<ImageStyle>;

Expand Down
9 changes: 3 additions & 6 deletions src/components/TransactionItemRow/ReceiptPreview/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ function ReceiptPreview({source, hovered, isEReceipt = false, transactionItem}:

const shouldShowImage = source && !(isEReceipt || isDistanceEReceipt || isPerDiemEReceipt);
const shouldShowDistanceEReceipt = isDistanceEReceipt && !isEReceipt && !isPerDiemEReceipt;
const sourceObject = typeof source === 'string' ? {uri: source} : source;

return ReactDOM.createPortal(
<Animated.View
Expand All @@ -127,12 +128,8 @@ function ReceiptPreview({source, hovered, isEReceipt = false, transactionItem}:
)}

<Image
source={typeof source === 'string' ? {uri: source} : source}
style={[
styles.w100,
{aspectRatio: imageAspectRatio ?? 1},
isLoading && {opacity: 0}, // hide until loaded
]}
source={sourceObject}
style={[styles.w100, {aspectRatio: imageAspectRatio ?? 1}]}
onLoadStart={() => {
if (isLoading) {
return;
Expand Down
Loading