I've got a counter array, containing 3 objects.
const [counter, setCounter] = useState([
{ id: 0, count: [] },
{ id: 1, count: [] },
{ id: 2, count: [] }
])
Theres then 3 buttons that when pressed call the following function.
const update = (i, text) => {
setCounter(currCount =>
currCount.id === i
? { id: i, count: [...counter[i].count, text] }
: currCount
);
};
The buttons pass "i" which is 0,1,2 corresponding to the 3 object ids and "text" which is the error text. The function should update the specific object from the array adding the new error text to that id's count array. I cant seem to get this to work though, it keeps returning undefined.
Any help is appreiciated.
setCounteraccepts an array and replacescounter(all the counters!). NIT: you should call the state objectcounters(in plural).