reactjs - 使用React Typescript的状态: Property does not exist on type 'IntrinsicAttributes & IntrinsicClassAttributes

标签 reactjs typescript jsx react-state react-typescript

我是使用Typescript进行React的新手,并且收到一条错误消息,指出

No overload matches this call. Overload 1 of 2, '(props: Readonly<{}>): IndexPage', gave the following error.

Type '{ notes: { 1: { _id: number; title: string; body: string; updatedAt: Date; }; }; }' is not assignable to type 'IntrinsicAttributes & IntrinsicClassAttributes & Readonly<{}> & Readonly<{ children?: ReactNode; }>'. Property 'notes' does not exist on type 'IntrinsicAttributes & IntrinsicClassAttributes & Readonly<{}> & Readonly<{ children?: ReactNode; }>'. Overload 2 of 2, '(props: {}, context?: any): IndexPage', gave the following error. Type '{ notes: { 1: { _id: number; title: string; body: string; updatedAt: Date; }; }; }' is not assignable to type 'IntrinsicAttributes & IntrinsicClassAttributes & Readonly<{}> & Readonly<{ children?: ReactNode; }>'. Property 'notes' does not exist on type 'IntrinsicAttributes & IntrinsicClassAttributes & Readonly<{}> & Readonly<{ children?: 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>
    )
  }
  }

最佳答案

您必须在组件上指定 Prop 和状态类型:

应用程序

type Note = {
  _id: number,
  title: string,
  body: string,
  updatedAt: Date
}
type Props = {/*props properties*/}
type State = {
  notes: {[key:number]: Note}
}
class App extends React.Component<Props, 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;

索引页
//You should extract Note in other file and import it on both components
type Note = {
  _id: number,
  title: string,
  body: string,
  updatedAt: Date
}
type Props = {
  notes: {[key:number]: Note}
}
export default class IndexPage extends React.Component<Props>{
  render(){
    const notes=Object.values(this.props.notes);
    return(
        <div>
            <h1> posts</h1>
            <h2> {notes[0].title}</h2>
        </div>
    )
  }
}

关于reactjs - 使用React Typescript的状态: Property does not exist on type 'IntrinsicAttributes & IntrinsicClassAttributes,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59595303/

相关文章:

javascript - 如何使用react-router-dom <Link> 防止事件冒泡?

javascript - 如何列出 props 变量中的项目?

node.js - 添加键值对数据

javascript - 简单测试但定位器无效 :-(

javascript - 如何处理返回 Promise 的函数

javascript - 在 PyCharm Community Edition 4.5.3 中启用 JSX 支持

javascript - this.setState 不是函数 React

javascript - npm install 类名在我的 li 标签中不起作用

reflection - TypeScript - 传递一个类作为参数,以及反射

javascript - 嵌入式表达式渲染为数组而不是单个元素