I have a pretty large object named cardRouteData (simplified in this example) and an array of integers named comulatedTimeOnSpotArr. I am trying to change the attribute value of "duration-with-stops" whenever comulatedTimeOnSpotArr changes.
const [comulatedTimeOnSpotArr,setComulatedTimeOnSpotArr] = useState([0,3600,20000])
const [cardRouteData,setCardRouteData] = useState({"duration":11450, "duration-with-stops":31145})
useEffect(() => {
if( comulatedTimeOnSpotArr!== undefined && comulatedTimeOnSpotArr.length >0){
let tempCardRootData = cardRouteData
tempCardRootData['duration-with-stops'] = cardRouteData.duration + comulatedTimeOnSpotArr[comulatedTimeOnSpotArr.length -1]
setCardRouteData(tempCardRootData)
}
}, [comulatedTimeOnSpotArr])
useEffect(() => {
if( cardRouteData!== undefined){
console.log('cardRouteData has changed')
}
}, [cardRouteData])
Now lets say I have an onClick function in my html that changes the last value of "comulatedTimeOnSpotArr" such as [0,3600,4000] Then useEffect should trigger and set the new value of "duration-with-stops" in "cardRouteData" to 15450. For some reason, it is not setting the new value, and it does not log the 'cardRouteData has changed' message to the console.