I am new at React. I have an array of objects that I created from the values of a Form. When I print it in the console it looks fine. but when I try to add that array to a different object, it changes its format in a very strange way. Here's the code:
const [ingredientes, setIngredientes] = useState([]);
const loadIngredient= (value) => {ingredients.push(value); setIngredients(ingredients);}
const LoadIngredient= () => {
loadIngredient(JSON.stringify({ingredient: ingredient, cant: parseInt(cantidad,10), unit: unidad}));
console.log('ingredients loaded: ', ingredients)
}
const RegistrarProducto = () => {
alert('Producto registrado: '+ JSON.stringify({
name: name,
image: image,
description: description,
receta: receta,
ingredients:ingredients}));
}
this is how it looks:
Producto registrado:
{"name":"Cookies","image":"","description":"The best cookies ever","receta":"this is a test only","ingredients":["{\"ingredient\":\"mantequilla\",\"cant\":100,\"unit\":\"gramos\"}","{\"ingredient\":\"azucar\",\"cant\":300,\"unit\":\"gramos\"}","{\"ingredient\":\"harina\",\"cant\":1,\"unit\":\"tazas\"}"]}
Can somebody explain to me why that happens? Thanks in advance and sorry for the spanglish.