简体   繁体   English

类型“IntrinsicAttributes 和 IntrinsicClassAttributes”上不存在属性“名称”<product> &amp; 只读&lt;{}&gt;'</product>

[英]Property 'name' does not exist on type 'IntrinsicAttributes & IntrinsicClassAttributes<Product> & Readonly<{}>'

         ```
   import React , {Component} from "react";
   import Product from "./product";
  export default class Shopping extends Component {
state={
products:[
{    id: 1,
    name: "Laptop", 
    price:"8000",
    photo:"https://picsum.photos/id/1010/60",
    quantity:4
    },
    {id:2,name:"HeadPhone", price:"9000",
photo:"https://picsum.photos/id/1011/60",
quantity:7}]
    }
   render(){
    return(
     <div>
       <h4>Shopping Cart</h4>
      <div>
        {this. state. products. map((prod)=>{
            return ( <Product 
            key={prod.id}
            name={prod.name} />);// this is where I am getting the error
           })}
        </div> 
     </div>
    )}


    }
    ```

Blockquote First time using react and don't understand what's the problem here. Blockquote 第一次使用 react 不明白这里有什么问题。 any help would be appreciated.任何帮助,将不胜感激。 I have imported the files correctly still not able to solve this.我已正确导入文件仍然无法解决此问题。

You can define the types for the state.您可以定义 state 的类型。

https://react-typescript-cheatsheet.netlify.app/docs/basic/getting-started/class_components/ https://react-typescript-cheatsheet.netlify.app/docs/basic/getting-started/class_components/

type MyProps = {
  // using `interface` is also ok
};
type MyState = {
  products:[
   {    
    id: number,
    name: string, 
    price: string,
    photo: string,
    quantity: number
   }
};
export default class Shopping extends Component<MyProps, MyState> {

  state : MyState={
   products:[
    {    
    id: 1,
    name: "Laptop", 
    price:"8000",
    photo:"https://picsum.photos/id/1010/60",
    quantity:4
    },
    {id:2,name:"HeadPhone", price:"9000",
     photo:"https://picsum.photos/id/1011/60",
     quantity:7}]
    }
   render(){
    return(
     <div>
       <h4>Shopping Cart</h4>
      <div>
        {this. state. products. map((prod)=>{
            return ( <Product 
            key={prod.id}
            name={prod.name} />);// this is where I am getting the error
           })}
        </div> 
     </div>
    )}


    }

暂无
暂无

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

相关问题 类型/ IntrinsicAttributes和IntrinsicClassAttributes上不存在React Typescript属性 - React Typescript property does not exist on type / IntrinsicAttributes & IntrinsicClassAttributes “IntrinsicAttributes &amp; MapContainerProps”类型上不存在属性“onClick” - Property 'onClick' does not exist on type 'IntrinsicAttributes & MapContainerProps' 类型 IntrinsicAttributes 和字符串 [] 上不存在属性“道具” - Property 'props' does not exist on type IntrinsicAttributes & string[] 类型“IntrinsicAttributes & Props”上不存在属性“...” - Property '...' does not exist on type 'IntrinsicAttributes & Props' React forwardRef - 类型 IntrinsicAttributes 上不存在属性 - React forwardRef - Property does not exist on type IntrinsicAttributes 如何解决“IntrinsicAttributes &amp; Function”类型上不存在的类型属性“”? - How to solve Type Property '' does not exist on type 'IntrinsicAttributes & Function'? React 属性在类型“IntrinsicAttributes”(自定义钩子)上不存在 - React Property does not exist on type 'IntrinsicAttributes' (custom hook) React TypeScript 和 ForwardRef - 类型“IntrinsicAttributes”上不存在属性“ref” - React TypeScript & ForwardRef - Property 'ref' does not exist on type 'IntrinsicAttributes 在“IntrinsicAttributes”类型错误中不存在获取属性“ref” - Getting Property 'ref' does not exist on type 'IntrinsicAttributes' error React native ref Property 'ref' 在类型'IntrinsicAttributes &amp; 上不存在 - React native ref Property 'ref' does not exist on type 'IntrinsicAttributes &
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM