reactjs - 类型 '{}' 不可分配给类型 'IntrinsicAttributes & IntrinsicClassAttributes<Search> & Readonly<{ children? : ReactNode; }>

标签 reactjs typescript

我对 React 和 TypeScript 还很陌生。我正在 Visual Studio 2017 中开发一个网页,其中有一个搜索组件,我想在我的主页组件中使用它。但是在将搜索导入我的主组件后,我得到一个编译时错误:

Type {} is not assignable to type 'IntrinsicAttributes & IntrinsicClassAttributes<Search> & Readonly<{ children?: ReactNode; }>  
Type {} is not assignable to type Readonly<SearchForm>  
 Property 'suggestionList' is missing in type {}

搜索组件代码:

export class Search extends React.Component<SearchForm> { //SearchForm is a class with string and an object of another class as properties
constructor() {
    super();
}

主页组件代码:

   export class Home extends React.Component<RouteComponentProps<{}>, {}> 
   {
   return <div>
        < Search /> // <=Getting error at this line
   <div>
   }

如果我删除 RouteComponentProps<{}>从 Home 组件,然后我在 route.tsx 文件中收到错误。我知道,我一定是做错了什么或犯了一些愚蠢的错误。我用谷歌搜索了这个,但没有得到任何有用的答案。有人可以告诉我如何解决这个问题吗?

最佳答案

如果您想创建功能组件,可以使用泛型来消除此错误。考虑这个例子。

interface SearchProps {
  suggestionList: any
}

export const Search: React.FC<SearchProps> = ({suggestionList}): JSX.Element => {
   return <div>...</div>
}

关于reactjs - 类型 '{}' 不可分配给类型 'IntrinsicAttributes & IntrinsicClassAttributes<Search> & Readonly<{ children? : ReactNode; }>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49315087/

相关文章:

javascript - 异步函数后未返回 React Native 元素

javascript - '$' 在 React 组件 props 中起什么作用?

Typescript 从返回回调函数值创建动态元组

angular - tsconfig.json 中的 Visual Studio 2017 Typescript baseUrl/paths 无法识别

html - Angular 2 - 动画过渡不起作用

TypeScript - 递归泛型和叶分支问题(终极 TicTacToe)

javascript - 为什么 Root 组件的状态没有改变?

reactjs - React-router-dom 推送

javascript - 如何在 React 中使用 i18n-iso-countries?

typescript - 如何强制对函数返回值进行 TypeScript 额外属性检查