I'm using ireact set state and trying to post data to my object that is nested with arrays.
Here is my default state:
const [data, setData] = useState({
working_hours: [
{
id: '',
description: '',
hours: '',
price: '',
total: '',
},
],
parts: [
{
id: '',
description: '',
unit: '',
quanity: '',
price: '',
total: '',
},
],
other: [
{
id: '',
description: '',
quanity: '',
price: '',
total: '',
},
],
});
So what I want to do is for example push an object to they array working_hours
and I don't know how to do it.
Here is my try:
const handleAdd = () => {
const test = {
id: 3,
description: 'This is a description',
hours: 4.2,
price: 500,
total: 4421,
};
setData({ ...data, working_hours: test });
};
But this is removing the last state and only replacing the first item.