[英]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.