I'm trying to update qty of product onClick event using useState in react, the data that I am updating is nested array-object-array
Below is the example of nested data I want to update the qty from it
let categoriesInfo = [
{
catid: 'category-1',
catName: 'Fun Stuff',
product: [
{
id : 1,
name: 'candy',
qty: 0,
},
{
id : 2,
name: 'cookie',
qty: 0,
}
]
},
{
catid: 'category-2',
catName: 'Fun Stuff2',
product: [
{
id : 1,
name: 'candy2',
qty: 0,
},
{
id : 2,
name: 'cookie2',
qty: 0,
}
]
}
]
I am using useState
const [productData, setProductData] = useState(categoriesInfo);
I am fetching the id and catid on click button from other component in the function x
const x = (data) =>{
console.log(productData)
const y = productData.map(obj => {
if (obj.catid == data.catid && obj.product[1-data.prodid].id == data.prodid) {
console.log(obj.product[1-data.prodid].name)
return {...obj.product[1-data.prodid], qty : 2}; //something like this
}
return obj;
});
setProductData(y);
}