I'm creating my own Image component and want to reuse React's image component ImageSourcePropType for my source/previewSource props.
Here's the component.
import React from 'react';
import { Image, ImageBackground } from 'react-native';
type ImageSourceProp = React.ComponentProps<typeof Image>.ImageSourcePropType; // HOW?
interface CachedImageProps {
source: ImageSourceProp;
previewSource: ImageSourceProp;
}
const CachedImage: React.FC<CachedImageProps> = ({ source, previewSource, ...remainingProps }) => {
// console.log('TRIGGERED CachedImage')
return (
<ImageBackground source={previewSource|| source} fadeDuration={0} {...remainingProps}>
<Image source={cachedSource} fadeDuration={0} {...remainingProps} />
</ImageBackground>
);
};
export default CachedImage;
How do I get the ImageSourcePropType?
OK, figured this one out. There are two ways to do it.
Access via name.
type ImageSourcePropType = React.ComponentProps<typeof Image>['source'];
Access via import.
import { ImageSourcePropType } from 'react-native';
(hat tip @kschaer)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With