599Item.js:12未熟悉的TypeError:product.map不是React的功能
我将我的第一个反应作为练习来理解,但我会发现此错误
599Item.js:12 uck offeraght typeError:product.map在component item.js on Line.js中不是一个函数
。 的产品对象进行的产品对象,
import React, {useState, useEffect} from "react";
import '../App.css';
import 'materialize-css/dist/css/materialize.css';
import Count from './ItemCount';
export const Item =(({product}) => {
const [sellProduct, setSellProduct] = useState(product);
useEffect(() => {
setSellProduct({product});
}, [product]);
const mensaje = () => {
alert ('Gracias por su compra');
}
return (
<>
{
sellProduct.map((item) => {
return(
<Count stock={item.stock} onAdd ={mensaje} >
<div id= {item.id}>
<h3>{item.name}</h3> - <small>{item.category}</small>
<img src= {item.picture} alt="Producto" className="itemImg" />
<p>{item.description}</p>
<p>{item.price}</p>
</div>
</Count>
)
})
}
</>
)
});
expor default Item;
有一个是我以提取
然后将其传递给子组件,该组件将道具传递给项目组件,我复制两个itemListContainer(parent)
import React, { useEffect, useState } from "react";
import '../App.css';
import 'materialize-css/dist/css/materialize.css';
import ItemList from './ItemList';
export const ItemListContainer =() => {
const [swSell,setSwSell] = useState([])
useEffect(() => {
fetch('https://fakestoreapi.com/products')
.then((res) => res.json())
.then((data) => {
setSwSell(data);
})
.catch((err) => {
console.log(err);
});
}, []);
return(
<div className="parent">
{
swSell.map((item) =>(
<div className="child" key={item.id}>
<ItemList item={swSell} />
</div>
))
}
</div>
)}
export default ItemListContainer;
的代码Itemellist(儿童),这将传递给项目组成部分,
import React from "react";
import '../App.css';
import 'materialize-css/dist/css/materialize.css';
import Item from './Item';
export const ItemList = (({item}) => {
return (
<div className="child">
{
item.map((item) =>(
<Item product= {item} />
))
}
</div>
)
});
export default ItemList;
感谢您的帮助
I'm putting together my first react as an exercise to understand but I get this error
599Item.js:12 Uncaught TypeError: product.map is not a function in component item.js on line 12
I copy the component item,js, it has a prop that is the product object
import React, {useState, useEffect} from "react";
import '../App.css';
import 'materialize-css/dist/css/materialize.css';
import Count from './ItemCount';
export const Item =(({product}) => {
const [sellProduct, setSellProduct] = useState(product);
useEffect(() => {
setSellProduct({product});
}, [product]);
const mensaje = () => {
alert ('Gracias por su compra');
}
return (
<>
{
sellProduct.map((item) => {
return(
<Count stock={item.stock} onAdd ={mensaje} >
<div id= {item.id}>
<h3>{item.name}</h3> - <small>{item.category}</small>
<img src= {item.picture} alt="Producto" className="itemImg" />
<p>{item.description}</p>
<p>{item.price}</p>
</div>
</Count>
)
})
}
</>
)
});
expor default Item;
I take the prop out in a fetch and pass it to the child component, which passes the prop to the items component and I copy the code of both
ItemListContainer(parent)
import React, { useEffect, useState } from "react";
import '../App.css';
import 'materialize-css/dist/css/materialize.css';
import ItemList from './ItemList';
export const ItemListContainer =() => {
const [swSell,setSwSell] = useState([])
useEffect(() => {
fetch('https://fakestoreapi.com/products')
.then((res) => res.json())
.then((data) => {
setSwSell(data);
})
.catch((err) => {
console.log(err);
});
}, []);
return(
<div className="parent">
{
swSell.map((item) =>(
<div className="child" key={item.id}>
<ItemList item={swSell} />
</div>
))
}
</div>
)}
export default ItemListContainer;
ItemList(child), this passes to the Item component
import React from "react";
import '../App.css';
import 'materialize-css/dist/css/materialize.css';
import Item from './Item';
export const ItemList = (({item}) => {
return (
<div className="child">
{
item.map((item) =>(
<Item product= {item} />
))
}
</div>
)
});
export default ItemList;
I appreciate your help
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在项目组件中而不是将
其替换为
In Item component instead of
replace it with
错误:
,因为
product
您将其设置为sellproduct
不是array
。它实际上是swsell
数组的项目(对象)。因此,首先,更改以下内容:
进入:
下一步,因为
swsell
状态是项目对象的集合,其中项目本身就是产品本身,那么您只需要两个组件: item item list < /strong>作为父母,项目作为渲染项目详细信息的孩子The error:
arise since the
product
props that you set assellProduct
is not anarray
. It is actually an item (object) ofswSell
array.So, first, change this:
into this:
Next, since the
swSell
state is a collection of item object where the item is the product itself, then you only need two component here: ItemList as parent and Item as the child that render the item details