With a button click I am able to get my state to update correctly from a child component. However, the button is not very useful to me. I need the state to update from the child of a child component to the function that runs from the parent component. I am pretty new to React, but this state update is challenging me.
In the parent functional component I have the state array like this
const [totals, setTotals] = useState({
0: 0,
1: 0,
2: 0,
3: 0,
4: 0,
5: 0,
6: 0,
7: 0,
8: 0,
9: 0,
10: 0,
11: 0
});
const handleCalculations = (key, value) => {
setTotals({
...totals,
[key]: totals[key] + value
});
}
I am prop Drilling handleCalculations down to a child component which passes it to another child where some calculations are done and passed back to the parent through useEffect. This looks like this...
useEffect(() => {
handleCalculations(index, paycheckCount);
}, []);
However, the state never gets updated except for the 11th index of the state array which gets 2. Could anyone please help me understand what I am doing wrong. I have included a link to a codesandbox with the full code so you can see all the code. Any help is appreciated