简体   繁体   English

使用Typescript在react中传递参数时出错

[英]Error when passing parameters in components in react using Typescript

When I pass the value parameter from App component to App2 component in react using typescript, it is giving an error 当我使用typescript将值参数从App组件传递给App2组件时,它会给出错误

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

The code of App.tsx is 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;

and the code of App2 component is :- 而App2组件的代码是: -

import * as React from "react";


class App2 extends React.Component{

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

        )
    }
}
export default App2;

You need to specify the type for the properties to tell the compiler what properties are valid on the App2 component and what their types are: 您需要指定属性的类型,以告诉编译器哪些属性在App2组件上有效以及它们的类型是什么:

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

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

    )
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在函数之间传递参数时出现TypeScript错误 - TypeScript error when passing parameters between functions 在 React Native 中将参数传递给组件 - Passing Parameters to Components in React Native React + TypeScript:将数组中的组件作为道具传递 - React + TypeScript: Passing components in an Array as props Typescript 类型用于传递函数和反应组件 - Typescript type for passing functions and react components 使用typescript在将prop从一个组件传递到另一个组件时发生错误,反之亦然 - Error Coming when passing props from one component to another and vice - versa in react using typescript 当尝试使用 typescript 定义功能组件以与样式组件反应时,会出现“没有重载匹配此调用”的错误。 - when try to define a functional component in react with styled-components using typescript get error of “No overload matches this call.” 使用 React useContext 和 useReducer 时出现打字稿错误 - Typescript error when using React useContext and useReducer 将表单输入传递给React组件时出错 - Error in passing form input to React components 使用超过 4 个参数时,Typescript .bind() 会出现类型错误 - Typescript .bind() gives type error when using more than 4 parameters 通过从子组件到父组件的函数来反应Redux传递参数 - React Redux passing parameters via functions from child components to parent
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM