使用React-Query从USEQUERY()返回产品
我有一个正在返回产品列表的API。我正在使用React-Query软件包获取数据,然后返回一张映射,然后又用辅助组件弹出每个产品。似乎无法获取数据。我已经确认API正在返回数据。问题从这里开始:
return products.map((product) => (
<ProductItem key={product.id} product={product} />
));
TypeError: Cannot read properties of undefined (reading 'map')
这是我编码的React Hook:
export default function ProductList() {
const { data: products, isLoading } = useQuery('Products', () =>
axios('/api/products').then((res) => res.data.products)
);
if (isLoading) return <LoadingSpinner />
return products.map((product) => (
<ProductItem key={product.id} product={product} />
));
}
这是辅助组件:
function ProductItem({ product }) {
const price = formatProductPrice(product);
return (
<div className="p-4 md:w-1/3">
<div className="h-full border-2 border-gray-800 rounded-lg overflow-hidden">
<Link to={`/${product.id}`}>
<img
className="lg:h-96 md:h-36 w-full object-cover object-center"
src={product.image}
alt={product.name}
/>
</Link>
<div className="p-6">
<h2 className="tracking-widest text-xs title-font font-medium text-gray-500 mb-1">
{product.category}
</h2>
<h1 className="title-font text-lg font-medium text-white mb-3">
{product.name}
</h1>
<p className="leading-relaxed mb-3">{product.description}</p>
<div className="flex items-center flex-wrap ">
<Link to={`/${product.id}`} className="text-indigo-400 inline-flex items-center md:mb-2 lg:mb-0">
See More
<svg
fill="none"
stroke="currentColor"
strokeLinecap="round"
strokeLinejoin="round"
strokeWidth="2"
className="w-4 h-4 ml-2"
viewBox="0 0 24 24"
>
<path d="M5 12h14M12 5l7 7-7 7"></path>
</svg>
</Link>
<span className="text-gray-500 mr-3 inline-flex items-center lg:ml-auto md:ml-0 ml-auto leading-none text-lg pr-3 py-1 border-gray-800 font-bold">
{price}
</span>
</div>
</div>
</div>
</div>
);
}
很可能是简单的事情,某些破坏性的错误,多元化或我引用数据的方式。 grr。
I have an api that is returning a list of products. I am using the react-query package to fetch the data and then return a map that in turn renders out each product with helper component. Can't seem to get data. I have confirmed that the API is returning data. Problem starts here:
return products.map((product) => (
<ProductItem key={product.id} product={product} />
));
TypeError: Cannot read properties of undefined (reading 'map')
This is the React Hook I coded:
export default function ProductList() {
const { data: products, isLoading } = useQuery('Products', () =>
axios('/api/products').then((res) => res.data.products)
);
if (isLoading) return <LoadingSpinner />
return products.map((product) => (
<ProductItem key={product.id} product={product} />
));
}
Here is the helper component:
function ProductItem({ product }) {
const price = formatProductPrice(product);
return (
<div className="p-4 md:w-1/3">
<div className="h-full border-2 border-gray-800 rounded-lg overflow-hidden">
<Link to={`/${product.id}`}>
<img
className="lg:h-96 md:h-36 w-full object-cover object-center"
src={product.image}
alt={product.name}
/>
</Link>
<div className="p-6">
<h2 className="tracking-widest text-xs title-font font-medium text-gray-500 mb-1">
{product.category}
</h2>
<h1 className="title-font text-lg font-medium text-white mb-3">
{product.name}
</h1>
<p className="leading-relaxed mb-3">{product.description}</p>
<div className="flex items-center flex-wrap ">
<Link to={`/${product.id}`} className="text-indigo-400 inline-flex items-center md:mb-2 lg:mb-0">
See More
<svg
fill="none"
stroke="currentColor"
strokeLinecap="round"
strokeLinejoin="round"
strokeWidth="2"
className="w-4 h-4 ml-2"
viewBox="0 0 24 24"
>
<path d="M5 12h14M12 5l7 7-7 7"></path>
</svg>
</Link>
<span className="text-gray-500 mr-3 inline-flex items-center lg:ml-auto md:ml-0 ml-auto leading-none text-lg pr-3 py-1 border-gray-800 font-bold">
{price}
</span>
</div>
</div>
</div>
</div>
);
}
It is most likely something simple, some destructuring error, plurality, or way that I am referencing the data. Grr.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论