I have an array like this
const [boxes, setBoxes] = useState([
{
title: "Background",
id: 0,
items: [],
},
]);
const [selectedBox, setSelectedBox] = useState(0);
I want to add a new object in items field and copy the past ones,
This is what I'm trying
setBoxes(prevState => {
return [
...prevState,
prevState[selectedBox].items: [...prevState[selectedBox].items, {title:"test"}]
]
})
Thanks a lot, have a nice day
Not sure what you are trying to do exactly due to the lack of information but you can try something like this:
setBoxes(prevState => {
const newBoxes = {
...prevState,
};
newBoxes[selectedBox].items = [
...newBoxes[selectedBox].items,
{ title: 'test' },
];
return newBoxes;
})
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.