javascript - for循环反应一次显示20个产品
问题描述
我正在尝试创建一个反应应用程序,该应用程序一次显示 20 个产品,一次添加,另外 20 个在另一个添加之前显示,所以我想做一个显示 20 个产品的 for 循环。我创建了一个 for 循环,但出现错误。
这是我的组件的渲染
render() {
const Prods = () => {
return (
<div>
<div className="row">
<div className="col-12">
<button onClick={this.sortPrice}>sort by price lower to higher</button>
<button onClick={this.sortSize}>sort by size small to big</button>
<button onClick={this.sortId}>sort by Id</button>
</div>
</div>
for (let i = 0; i < 20; i++) {
{products.map(product =>
<div key={product.id} className="row">
<div className="col-3">
<p> Price: ${(product.price/100).toFixed(2)}</p>
</div>
<div className="col-3">
<p style={{fontSize: `${product.size}px`}} > {product.face}</p>
</div>
</div>
)}
}
<p>"~END OF CATALOG~"</p>
</div>
);
};
const { products, isLoading, error } = this.state;
if (error) {
return <p>{error.message}</p>;
}
if (isLoading) {
return <Loading />;
}
return (
<Prods />
);
}
}
这是我的错误:
解析错误:意外的令牌
102 | </div>
103 | </div>
> 104 | for (let i = 0; i < 20; i++) {
| ^
105 | {products.map(product =>
我尝试使用数组在外部创建一个函数以在渲染外部执行循环,但出现了不同的错误。这是我对具有 for 循环的函数的组件
createDisplay = () => {
let display = []
for (let i = 0; i < 20; i++) {
dislay.push({products.map(product =>
<div key={product.id} className="row">
<div className="col-3">
<p> Price: ${(product.price/100).toFixed(2)}</p>
</div>
<div className="col-3">
<p style={{fontSize: `${product.size}px`}} > {product.face}</p>
</div>
</div>
)})
}
return display
}
render() {
const Prods = () => {
return (
<div>
<div className="row">
<div className="col-12">
<button onClick={this.sortPrice}>sort by price lower to higher</button>
<button onClick={this.sortSize}>sort by size small to big</button>
<button onClick={this.sortId}>sort by Id</button>
</div>
</div>
<div>
{this.createDisplay()}
</div>
<p>"~END OF CATALOG~"</p>
</div>
);
};
const { products, isLoading, error } = this.state;
if (error) {
return <p>{error.message}</p>;
}
if (isLoading) {
return <Loading />;
}
return (
<Prods />
);
}
}
有了这个我得到下一个错误解析错误:
Unexpected token, expected ","
92 |
93 | for (let i = 0; i < 20; i++) {
> 94 | dislay.push({products.map(product =>
| ^
95 |
96 |
97 | <div key={product.id} className="row">
我是新手,所以我有点迷茫。
解决方案
您在 JSX 中的代码需要包含在{}
{ for (let i = 0; i < 20; i++) {
{products.map(product =>
<div key={product.id} className="row">
<div className="col-3">
<p> Price: ${(product.price/100).toFixed(2)}</p>
</div>
<div className="col-3">
<p style={{fontSize: `${product.size}px`}} > {product.face}</p>
</div>
</div>
)}
}
}
对于第二个
你试图用 20 个 JSX 元素推送一个对象:
createDisplay = () => {
let display = [];
for (let i = 0; i < 20; i++) {
//map returns an array
let productElements = products.map(product =>
<div key={product.id} className="row">
<div className="col-3">
<p> Price: ${(product.price/100).toFixed(2)}</p>
</div>
<div className="col-3">
<p style={{fontSize: `${product.size}px`}} > {product.face}</p>
</div>
</div>
);
display.push(...productElements); //use spread elements
}
return display
}
在 for 循环中映射也不是您想要做的。删除 for 循环,只使用 map。它返回映射操作结果的数组。由于 for 循环,您所做的是使渲染需要很长时间,因为它会创建并填充 20 个项目 20 次。
{
products.map(product =>
<div key={product.id} className="row">
<div className="col-3">
<p> Price: ${(product.price/100).toFixed(2)}</p>
</div>
<div className="col-3">
<p style={{fontSize: `${product.size}px`}} > {product.face}</p>
</div>
</div>
)
}
你可以用上面的方法做所有事情,不需要 for 循环。
推荐阅读
- angular - xng-breadcrumb 在“shouldReuseRoute = false”上中断
- plotly - 将单个标签嵌入到 plotly express 直方图中
- selenium-webdriver - 尝试通过詹金斯运行 chrome 无浏览器执行时出现未处理的请求错误
- azure-blob-storage - 从 blob 存储到 azure 表存储的增量负载
- php - PHP mail() 在“Microsoft Azure VM”中给出 Network is unreachable
- javascript - 如何检查一个字符串是否至少包含一个 JavaScript 字母?
- segmentation-fault - 由于分段错误错误,KDB 变得无响应
- php - 如何在 php 可重用表单中发送多复选框输入值?
- c - 为什么我的字符串在初始化时会被连接起来?
- php - 我的 CodeIgniter 站点存在问题,我在该站点中遇到了索引问题