[英]How do you extract a specific TypeScript prop type from a React component?
I'm creating my own Image component and want to reuse React's image component ImageSourcePropType
for my source/previewSource props.我正在创建自己的 Image 组件,并希望将 React 的图像组件
ImageSourcePropType
用于我的 source/previewSource 道具。
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
?我如何获得
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
访问。
import { ImageSourcePropType } from 'react-native';
(hat tip @kschaer) (帽子提示@kschaer)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.