Reactjs 我可以为包装在HOC中的组件执行typescript道具验证吗?
我正在用React HOC编写我的组件,但在使用typescript验证道具时,它给出了错误。 错误如下 类型“{Type:string;imgUrl:any;vidUrl:any;}”不可分配 输入“内在属性和选择”{ 子节点?:ReactNode;} 我的组件的外观(示例):Reactjs 我可以为包装在HOC中的组件执行typescript道具验证吗?,reactjs,typescript,react-props,react-tsx,Reactjs,Typescript,React Props,React Tsx,我正在用React HOC编写我的组件,但在使用typescript验证道具时,它给出了错误。 错误如下 类型“{Type:string;imgUrl:any;vidUrl:any;}”不可分配 输入“内在属性和选择”{ 子节点?:ReactNode;} 我的组件的外观(示例): 类型道具={ 类型:字符串; imgUrl:任何; vidUrl:任何; } 常量组件=({type,imgUrl,vidUrl}:Props)=>( ..... ..... ) 导出带翻译的解除争议(“通用”)(组件
类型道具={
类型:字符串;
imgUrl:任何;
vidUrl:任何;
}
常量组件=({type,imgUrl,vidUrl}:Props)=>(
.....
.....
)
导出带翻译的解除争议(“通用”)(组件);
您必须为组件提供正确的类型
import { withTranslation, WithTranslation } from 'react-i18next';
import React from "react";
type Props = {
type: string;
imgUrl: any;
vidUrl: any;
}
const Component: React.FC<Props & WithTranslation> = ({type,imgUrl,vidUrl})=> (
<div>.....
.....
</div>
)
const withTranslationComponent = withTranslation('common')(Component);
export default withTranslationComponent;
从'react-i18next'导入{withTranslation,withTranslation};
从“React”导入React;
类型道具={
类型:字符串;
imgUrl:任何;
vidUrl:任何;
}
常量组件:React.FC=({type,imgUrl,vidUrl})=>(
.....
.....
)
常量withTranslationComponent=withTranslation('common')(组件);
使用TranslationComponent导出默认值;
噢,所以我必须将我的类型与HOC的类型结合起来。我的是next-i18n-next,所以我要试试。谢谢作为更新,我想补充一点,在功能组件中使用UseTransation钩子很好。
import { withTranslation, WithTranslation } from 'react-i18next';
import React from "react";
type Props = {
type: string;
imgUrl: any;
vidUrl: any;
}
const Component: React.FC<Props & WithTranslation> = ({type,imgUrl,vidUrl})=> (
<div>.....
.....
</div>
)
const withTranslationComponent = withTranslation('common')(Component);
export default withTranslationComponent;