我可以对包装在HOC中的组件进行打字稿道具验证吗?

时间:2019-11-15 05:53:05

标签: reactjs typescript react-props react-tsx

我正在用React HOC编写组件,但是使用打字稿验证道具时却出现错误。 错误如下

  

Type'{type:string; imgUrl:任何; vidUrl:任意; }'不可分配   键入“ IntrinsicAttributes&Pick&{   孩子们?:ReactNode;}'。

我的组件是什么样的(示例):

type Props = {
type: string;
imgUrl: any;
vidUrl: any;
}
const Component = ({type,imgUrl,vidUrl}:Props)=> (
<div>.....
.....
</div>
)
export defualt withTranslation('common')(Component);

1 个答案:

答案 0 :(得分:0)

您必须提供正确的组件类型。

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;