Can not understand why filter is not working.
The function get an object. If there is the same object in the state, the function delete this object from the state. If not, add to the state.
The problem is that the object rewrites the state, but not add it to the state.
For example, object {a: 1, b: 2}
const [products, setProducts] = useState<any[]>([]);
const addObject = (data) => {
const sameData = !!products.find((item) => item.id === data.id);
const deleteData = products.filter((item) => item.id !== data.id);
const newData = sameData ? deleteData : [...products, data];
return setCheckedRoles(newData);
};
As a result I get not [{a: 1, b: 2}, {a: 2, b: 4}, {a: 8, b: 3}];
but only one object that come to function [{a: 1, b: 2}]