使用React Typescript的状态:类型'IntrinsicAttributes和IntrinsicClassAttributes不存在属性

时间:2020-01-04 22:14:41

标签: reactjs typescript jsx react-state react-typescript

我是使用Typescript进行React的新手,但出现错误提示

没有重载匹配此调用。重载1,共2个,'(props: Readonly <{}>):IndexPage”,出现以下错误。

键入'{注:{1:{_id:数字; title:字符串正文:字符串; UpdatedAt:日期; }; }; }'不可分配给type 'IntrinsicAttributes&IntrinsicClassAttributes& Readonly <{}>和Readonly <{子代?:ReactNode; }>'。 属性'notes'在类型'IntrinsicAttributes&IntrinsicClassAttributes&Readonly <{}>&Readonly <{上不存在 孩子们?:ReactNode; }>'。重载2之2,'(props:{},context ?: 任何):IndexPage',给出以下错误。 键入'{注意:{1:{{_id:数字; title:字符串正文:字符串; UpdatedAt:日期; }; }; }'不可分配给type 'IntrinsicAttributes&IntrinsicClassAttributes& Readonly <{}>和Readonly <{子代?:ReactNode; }>'。 属性'notes'在类型'IntrinsicAttributes&IntrinsicClassAttributes&Readonly <{}>&Readonly <{上不存在 孩子们?:ReactNode; }>'。

  **App.tsx**
//import statements 

  type Note={
notes: {
    1: {
_id: number;
body:string;
title:string;
updatedAt: Date

    }
} }
type State={notes: {[key:number]: Note} }
class App extends React.Component <State> {
state={
    notes: {
        1: {
            _id:1,
            title: "hello world",
            body: "this is the body",
            updatedAt:new Date()
        }
      }
   }
   render(){
   return (
    <div className="App">
        <Nav/>
        <Headers/>
        <IndexPage notes = {this.state.notes}/>

    </div>
  );
}
 }
export default App;

================================================ ======= Index.tsx:

import React from 'react';

export default class IndexPage extends React.Component{
render(){
    const notes=Object.values(this.props.notes);
    return(
        <div>
            <h1> posts</h1>
            <h2> {notes[0].title}</h2>
        </div>
    )
  }
  }

0 个答案:

没有答案