-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
BaseImage.tsx
32 lines (27 loc) · 1.03 KB
/
BaseImage.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import React, {useCallback} from 'react';
import {Image as RNImage} from 'react-native';
import type {ImageLoadEventData} from 'react-native';
import type {BaseImageProps} from './types';
function BaseImage({onLoad, ...props}: BaseImageProps) {
const imageLoadedSuccessfully = useCallback(
(event: {nativeEvent: ImageLoadEventData}) => {
if (!onLoad) {
return;
}
// We override `onLoad`, so both web and native have the same signature
const {width, height} = event.nativeEvent.source;
onLoad({nativeEvent: {width, height}});
},
[onLoad],
);
return (
<RNImage
// Only subscribe to onLoad when a handler is provided to avoid unnecessary event registrations, optimizing performance.
onLoad={onLoad ? imageLoadedSuccessfully : undefined}
// eslint-disable-next-line react/jsx-props-no-spreading
{...props}
/>
);
}
BaseImage.displayName = 'BaseImage';
export default BaseImage;