Issue
I am trying to push to my react state every time a submit my form, but after 2 values stored the state start to get overwriten. Function code:
function addOrder(values: any) {
let ordersArray: any[] = [];
ordersArray.push({
id: uuidv4(),
client: clientsField?.label,
catalog: catalogField?.label,
product: productField.label,
regularPrice: Number(productField.regularPrice),
discountPrice: Number(productField.discountPrice),
amount: values.amount,
totalValue: values.amount * Number(productField.regularPrice),
});
setPedido(pedidosArray);
}
update: I can submit the form but my state is still getting overwritten
Solution
Do like this there is no need to an extra array:
function adcionarPedido(values: any) {
let pedidosArray = [...pedidos];
pedidosArray.push({
id: uuidv4(),
cliente: clientesField?.label,
catalogo: catalogoField?.label,
produto: produtoField.label,
valorRegular: Number(produtoField.valorRegular),
valorOferta: Number(produtoField.valorOferta),
quantidade: values.quantidade,
valorBruto: values.quantidade * Number(produtoField.valorRegular),
});
setPedido(pedidosArray);
}
Answered By - Jamal
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.