I've tried different solutions to similar problems provided here and none of them worked, whenever i try to add to an array or set a state to a new array using hooks, i always get an error caused by too many re renders. no matter if i wrap my logic in arrow functions, or try adding to prevState, no matter if i use concat, push, map, filter always the same result. So i thought to create my array first, before even declaring the state variable, like that:
const newCompletedItems = data.map(obj => obj.completed ? obj.id : null).filter(item => item);
const [completedItems, setCompletedItems] = useState(newCompletedItems);
No error messages there, but i get an array with 0 items, while newCompletedItems is an array with multiple items (as expected).
Basically i need to set the state to an array that is an outcome of a function and then in future i'll need to setCompletedItems again, by adding to/ substracting from completedItems array.