React state is undefined , even though console.log shows it

  javascript, reactjs

I have researched other cases when the state was unidentified but I am still unsuccessfully trying to subtract data from UI actions that is in the format [{},{}…]. I have managed to add to the array , using this code, which in the same time also computes the total for the item (subTotal and products are props from a child component got through a callback function):

const updateTotalPriceAndUpdatePieChartData = (subTotal,product) => {
        //from here   
        setPieChartData([...pieChartData,{product,subTotal}])
        //until up, we handle what data we need for the pieChart
        setTotal(total => total + (Number.isFinite(subTotal) ? subTotal : 0))
        console.log("TOTAL WAS COMPUTED")
    }

And this is the state that holds the array:

const [pieChartData,setPieChartData]=React.useState([])

Yet when I try to delte an object from the state array (when an item is also deleted), I try the following :

const substractSubTotalAndSubstractTotalForPieChart = (subTotal,product,pieChartData) => {
    setTotal(total - subTotal)
    const lastPieChartData=pieChartData.filter(item => item !={subTotal,product})
    setPieChartData(lastPieChartData)
}

It says that pieChartData is unidentified. Could you please let me know what I can do?

Source: Ask Javascript Questions

LEAVE A COMMENT