I made a similar question related with arrays and maps but only half got answered which is why I'm making this one apart.
I have this code (Only relevant info):
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>
I know that in javascript you can do the following:
Array(5).fill(2)
//=> [2, 2, 2, 2, 2]
Is there a way to do something similar in React? because what I want to achieve is the following:
"cantidad" will always start as 1 and then grant the ability to the user to change that amount +1 or -1 depending on how many of an item he/she wants but I do not know how to set up all the values to 1, with that being said, someone already guide me before telling me that by adding the index value on the map I can control them individually I fail to understand how to apply that on my code because I would like to send the current index and the current value of that index to the
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);
}
};
functions that I have made, this is how is printing atm, I kind of understand it how to do it if you already set up an X amount of values in the array but not when the amount of values are based on the amount of times the map repeats I try using map.lenght but didn't work any advice/tips are welcome
This is the whole code
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
This is how it looks like and what does cantidad is showing on the logs
See Question&Answers more detail:
os