Accepted answer

Replace this line:


With this

setIncomes([...incomes, income]);

The .push method returns the length of the array, not the actual array. You can use the spread operator to spread the current array and then add on the new item to the end of it.

Doing this should also work:



It's possible you're getting that error because the data coming back from your API isn't an array. Judging by your code, I'm guessing you're expecting a key/value map, which in JS is an object. You might be able to use Object.keys(incomes).map(...), but without knowing the specific response format, I can't say for sure.

There are 2 other issues with your code:

First, you can't push onto incomes because it's a React state array. Instead, you need to use the setIncomes callback...

setIncomes([...incomes, income])

Additionally, the way you're using Object.keys and Object.values is not correct. Again, can't say what the correct way is without knowing specifics of your response format.

Related Query

More Query from same tag