【发布时间】:2021-09-17 22:14:37
【问题描述】:
我提出了一个与数组和地图相关的类似问题,但只有一半得到了回答,这就是我将这个分开的原因。
我有这个代码(只有相关信息):
const [libros, setLibros] = useState([]);
const [cantidad, setCantidad] = useState([1]);
//This is not finished because I'm still stuck
const mas = (index, value) => {
setCantidad(cantidad[index] + 1);
};
const menos = (index, value) => {
if (cantidad[index] > 0){
setCantidad(cantidad[index] - 1);
}
else {
window.alert("Sorry, Zero limit reached");
setCantidad(0);
}
};
<tbody>
{libros.map((l, index) => (
<tr >
<td>
<button onClick = {() => mas(index)}/>
{cantidad[index]}
<button onClick = {() => menos(index)}/>
</td>
<td>{l.grado}</td>
<td >
<input onChange = {(event) => {
let checked = event.target.checked;
}}
type="checkbox" checked = "">
//I know this check is not working properly I'm strill trying to figure out this one
</input>
{l.descripcion}
</td>
<td >{l.editorial}</td>
<td >${parseFloat(l.precio).toFixed(2) * cantidad[index]}</td>
</tr>
))}
</tbody>
我知道在 javascript 中您可以执行以下操作:
Array(5).fill(2)
//=> [2, 2, 2, 2, 2]
有没有办法在 React 中做类似的事情?因为我想要实现的是以下内容: “cantidad”将始终以 1 开头,然后授予用户根据他/她想要的项目数量更改该数量 +1 或 -1 的能力,但我不知道如何将所有值设置为 1话虽如此,在告诉我通过在地图上添加索引值我可以单独控制它们之前已经有人指导我我不明白如何在我的代码上应用它,因为我想发送当前索引和当前该索引的值为
const mas = (index, value) => {
setCantidad(cantidad[index] + 1);
};
const menos = (index, value) => {
if (cantidad[index] > 0){
setCantidad(cantidad[index] - 1);
}
else {
window.alert("Sorry, Zero limit reached");
setCantidad(0);
}
};
我制作的函数,这就是打印 atm 的方式,如果您已经在数组中设置了 X 数量的值,但当值的数量基于数量时,我有点理解如何做到这一点地图重复的次数我尝试使用 map.lenght 但没有奏效欢迎任何建议/提示
这是完整的代码
import React, { useState, useEffect } from 'react'
import { auth, db } from './firebase';
import { useHistory } from 'react-router-dom';
import { Checkbox } from '@material-ui/core';
function CrearPedidos({user}) {
const [libros, setLibros] = useState([]);
const [cantidad, setCantidad] = useState(new Array(libros.length).fill(1);
const history = useHistory("");
const [totalPrice, setTotalPrice] = useState();
const librosRef = db.collection('libros');
const queryRef = librosRef.where('grado', '==', '4° Grado');
console.log(queryRef)
useEffect(() => {
queryRef.orderBy("precio")
.get()
.then((snapshot) => {
const tempData = [];
snapshot.forEach((doc) => {
const data = doc.data();
tempData.push(data);
});
setLibros(tempData);
});
}, []);
const mas = (index) => {
setCantidad(cantidad[index] + 1);
};
const menos = (index) => {
if (cantidad[index] > 0){
setCantidad(cantidad[index] - 1);
}
else {
window.alert("Sorry, Zero limit reached");
setCantidad(0);
}
};
return (
<div className="listado_Pedidos">
<div className="estudiantes_container">
<h1 className = "estudiantes_container_h1">Estudiante: {user.displayName}</h1>
<h1 className = "estudiantes_container_h1">Libros Nuevos</h1>
<div className ="tableContainer">
<table>
<thead>
<tr className="Lista">
<th>Cantidad</th>
<th>Grado</th>
<th>Descripcion</th>
<th>Editorial</th>
<th>Precio</th>
</tr>
</thead>
<tbody>
{libros.map((l, index) => (
<tr >
<td>
<button onClick = {() => mas(index)}/>
{cantidad[index]}
<button onClick = {() => menos(index)}/>
</td>
<td>{l.grado}</td>
<td >
<input onChange = {(event) => {
let checked = event.target.checked;
}}
type="checkbox" checked = "">
</input>
{l.descripcion}
</td>
<td >{l.editorial}</td>
<td >${parseFloat(l.precio).toFixed(2) * cantidad[index]}</td>
</tr>
))}
</tbody>
</table>
</div>
<div className="space" />
<button onClick="{realizarPedidos}" className = "crear_estudiante_boton">Realizar Pedidos</button>
<div className="space" />
</div>
</div>
)
}
export default CrearPedidos
这就是它的样子以及 Cantidad 在日志上显示的内容
【问题讨论】:
-
我注意到的第一件事是
const [cantidad, setCantidad] = useState([1]),然后再往下一点:setCantidad(cantidad[index] + 1);。这是故意的吗?您将其初始化为数组 ([]),编号为1,但随后您将类型更改为数字,而不是数组。 -
呃,我在 React 中的编码技能并不是很粗糙/很差,所以我只是在尝试。