javascript - 反应处理状态
问题描述
我正在尝试制作一个电子商务应用程序,并且我刚刚启动了该项目,但我不断收到错误消息。每次我尝试从我的数据组件中获取信息然后通过它进行映射时,我都会得到“无法读取未定义的属性 'map'”。有人可以帮我吗?以下是组件:
import React, { useState, useEffect } from 'react';
import './App.css';
import { Routes } from './routes'
import { Main } from './components/Main'
import { storeProducts } from './data/data'
function App() {
const [items, setItems] = useState([])
useEffect(() => {
setItems(storeProducts)
}, [])
return (
<div className="App">
<Routes />
<Main items={items} />
</div>
);
}
export default App;
import React from 'react'
import { ItemComponent } from './ItemComponent'
export const Main = ({items}) => {
const products = items.map(() => <ItemComponent />)
return (
<div>
{products}
</div>
)
}
import React from 'react'
export const ItemComponent = ({details}) => {
return (
<div>
Hello Item
</div>
)
}
使用的数据
export const storeProducts = [
{
id: 1,
title: "Google Pixel - Black",
img: "img/product-1.png",
price: 10,
company: "GOOGLE",
info:
"Lorem ipsum dolor amet offal butcher quinoa sustainable gastropub, echo park actually green juice sriracha paleo. Brooklyn sriracha semiotics, DIY coloring book mixtape craft beer sartorial hella blue bottle. Tote bag wolf authentic try-hard put a bird on it mumblecore. Unicorn lumbersexual master cleanse blog hella VHS, vaporware sartorial church-key cardigan single-origin coffee lo-fi organic asymmetrical. Taxidermy semiotics celiac stumptown scenester normcore, ethical helvetica photo booth gentrify.",
inCart: false,
count: 0,
total: 0
},
{
id: 2,
title: "Samsung S7",
img: "img/product-2.png",
price: 16,
company: "SAMSUNG",
info:
"Lorem ipsum dolor amet offal butcher quinoa sustainable gastropub, echo park actually green juice sriracha paleo. Brooklyn sriracha semiotics, DIY coloring book mixtape craft beer sartorial hella blue bottle. Tote bag wolf authentic try-hard put a bird on it mumblecore. Unicorn lumbersexual master cleanse blog hella VHS, vaporware sartorial church-key cardigan single-origin coffee lo-fi organic asymmetrical. Taxidermy semiotics celiac stumptown scenester normcore, ethical helvetica photo booth gentrify.",
inCart: false,
count: 0,
total: 0
},
{
id: 3,
title: "HTC 10 - Black",
img: "img/product-3.png",
price: 8,
company: "htc",
info:
"Lorem ipsum dolor amet offal butcher quinoa sustainable gastropub, echo park actually green juice sriracha paleo. Brooklyn sriracha semiotics, DIY coloring book mixtape craft beer sartorial hella blue bottle. Tote bag wolf authentic try-hard put a bird on it mumblecore. Unicorn lumbersexual master cleanse blog hella VHS, vaporware sartorial church-key cardigan single-origin coffee lo-fi organic asymmetrical. Taxidermy semiotics celiac stumptown scenester normcore, ethical helvetica photo booth gentrify.",
inCart: false,
count: 0,
total: 0
},
{
id: 4,
title: "HTC 10 - White",
img: "img/product-4.png",
price: 18,
company: "htc",
info:
"Lorem ipsum dolor amet offal butcher quinoa sustainable gastropub, echo park actually green juice sriracha paleo. Brooklyn sriracha semiotics, DIY coloring book mixtape craft beer sartorial hella blue bottle. Tote bag wolf authentic try-hard put a bird on it mumblecore. Unicorn lumbersexual master cleanse blog hella VHS, vaporware sartorial church-key cardigan single-origin coffee lo-fi organic asymmetrical. Taxidermy semiotics celiac stumptown scenester normcore, ethical helvetica photo booth gentrify.",
inCart: false,
count: 0,
total: 0
},
{
id: 5,
title: "HTC Desire 626s",
img: "img/product-5.png",
price: 24,
company: "htc",
info:
"Lorem ipsum dolor amet offal butcher quinoa sustainable gastropub, echo park actually green juice sriracha paleo. Brooklyn sriracha semiotics, DIY coloring book mixtape craft beer sartorial hella blue bottle. Tote bag wolf authentic try-hard put a bird on it mumblecore. Unicorn lumbersexual master cleanse blog hella VHS, vaporware sartorial church-key cardigan single-origin coffee lo-fi organic asymmetrical. Taxidermy semiotics celiac stumptown scenester normcore, ethical helvetica photo booth gentrify.",
inCart: false,
count: 0,
total: 0
},
{
id: 6,
title: "Vintage Iphone",
img: "img/product-6.png",
price: 17,
company: "apple",
info:
"Lorem ipsum dolor amet offal butcher quinoa sustainable gastropub, echo park actually green juice sriracha paleo. Brooklyn sriracha semiotics, DIY coloring book mixtape craft beer sartorial hella blue bottle. Tote bag wolf authentic try-hard put a bird on it mumblecore. Unicorn lumbersexual master cleanse blog hella VHS, vaporware sartorial church-key cardigan single-origin coffee lo-fi organic asymmetrical. Taxidermy semiotics celiac stumptown scenester normcore, ethical helvetica photo booth gentrify.",
inCart: false,
count: 0,
total: 0
},
{
id: 7,
title: "Iphone 7",
img: "img/product-7.png",
price: 30,
company: "apple",
info:
"Lorem ipsum dolor amet offal butcher quinoa sustainable gastropub, echo park actually green juice sriracha paleo. Brooklyn sriracha semiotics, DIY coloring book mixtape craft beer sartorial hella blue bottle. Tote bag wolf authentic try-hard put a bird on it mumblecore. Unicorn lumbersexual master cleanse blog hella VHS, vaporware sartorial church-key cardigan single-origin coffee lo-fi organic asymmetrical. Taxidermy semiotics celiac stumptown scenester normcore, ethical helvetica photo booth gentrify.",
inCart: false,
count: 0,
total: 0
},
{
id: 8,
title: "Smashed Iphone",
img: "img/product-8.png",
price: 2,
company: "apple",
info:
"Lorem ipsum dolor amet offal butcher quinoa sustainable gastropub, echo park actually green juice sriracha paleo. Brooklyn sriracha semiotics, DIY coloring book mixtape craft beer sartorial hella blue bottle. Tote bag wolf authentic try-hard put a bird on it mumblecore. Unicorn lumbersexual master cleanse blog hella VHS, vaporware sartorial church-key cardigan single-origin coffee lo-fi organic asymmetrical. Taxidermy semiotics celiac stumptown scenester normcore, ethical helvetica photo booth gentrify.",
inCart: false,
count: 0,
total: 0
}
];
解决方案
您的错误意味着您对具有未定义值的变量使用.map函数。在这里,我有一些反馈可以改进您的代码:
无需在 App 组件中使用 useEffect:
function App() {
const [items, setItems] = useState(storeProducts);
return (
<div className="App">
<Routes />
<Main items={items} />
</div>
);
}
主要成分:
export const Main = ({items}) => {
const products = (items || []).map(() => <ItemComponent />)
return (
<div>
{products}
</div>
)
}
项目 || []意思是:如果项目未定义,则使用空数组。然后不再出现错误“无法读取未定义的属性'map'”。
推荐阅读
- java - 为什么需要单元测试?
- python - 在上下文管理器中运行子命令
- azure - 如何加速 Azure Service Fabric 上的调试?
- mongodb - 如何禁用 mongod 日志预分配
- javascript - 用另一个数组过滤一个数组
- r - 将一个数据集中的变量分配给另一个数据集中的多个字段
- php - 在 Wordpress 的查询集中使用运算符 LIKE
- firebase - Flutter 中的云函数异常
- python - pd.read_excel() 用 `nan` 字符串替换空格, pd.read_csv() 使用 numpy.nan
- batch-file - 通过批处理文件调用带有变量的命令