使用Typescript在React中传递组件中的参数时出错

时间:2018-07-16 14:12:50

标签: javascript reactjs typescript

当我使用打字稿在React中将值参数从App组件传递到App2组件时,它给出了错误

Property 'value' does not exist on type 'IntrinsicAttributes & IntrinsicClassAttributes<App2> & Readonly<{ children?: ReactNode; }> & Read...

App.tsx的代码是

import * as React from "react";
import './App.css';
 import App2 from './App2';


class App extends React.Component<any,any>{

  public render(){
    return(
      <div>
          <h1>
            <App2 value = {5}/>
          </h1>
        </div>
    )
  }

}
export default App;

,App2组件的代码为:-

import * as React from "react";


class App2 extends React.Component{

    public render(){
        return(
            <div>
            <h4>Hello world</h4>
           </div>

        )
    }
}
export default App2;

1 个答案:

答案 0 :(得分:2)

您需要指定属性的类型,以告诉编译器App2组件上哪些属性有效以及它们的类型是什么:

class App2 extends React.Component<{ value: number }>{

  public render() {
    return (
      <div>
        <h4>Hello world</h4>
      </div>

    )
  }
}