import React, { type FunctionComponent, memo, useEffect, useState, } from "react"; import { ActivityIndicator, Image, ImageBackground, type ImageStyle, } from "react-native"; type MDImageProps = { uri: string; label?: string; alt?: string; style?: ImageStyle; }; type MDImageState = { isLoading: boolean; aspectRatio: number | undefined; }; const MDImage: FunctionComponent = ({ uri, label, alt = "Image", style, }) => { const [imageState, setImageState] = useState({ isLoading: true, aspectRatio: undefined, }); /** * Fetches image dimension * Sets aspect ratio if resolved */ const fetchOriginalSizeFromRemoteImage = () => { Image.getSize( uri, (width: number, height: number) => { if (width > 0 && height > 0) { setImageState({ isLoading: false, aspectRatio: width / height }); } else { setImageState({ isLoading: false, aspectRatio: undefined }); } }, () => { setImageState((current) => { return { ...current, isLoading: false, }; }); }, ); }; useEffect(() => { fetchOriginalSizeFromRemoteImage(); }); return ( {imageState.isLoading ? ( ) : null} ); }; export default memo(MDImage);