[英]setState containing object=>array=> objects
What i have is我所拥有的是
const [productData, setproductData] = useState({
title: "",
description: "",
price: "",
selectedFile: "",
images: [{ _id: "", urls: "" }],
});
How i will change the values inside IMAGES object.我将如何更改IMAGES object 中的值。 I want to change id and Urls.我想更改 id 和 Urls。
I have tried the following methods but unsuccessful.我尝试了以下方法但不成功。
setproductData({
...productData,
images: [...productData.images._id, i], //where i is any id or number
});
but when i try to execute it it says productData.images.id is not iterable.但是当我尝试执行它时,它说productData.images.id不可迭代。 Can any help plz...有什么帮助请...
try this out试试这个
setproductData({
...productData,
images: [{...productData.images[0], _id: i}],
});
if you want to modify selected id just use如果你想修改选定的 id 只需使用
setproductData({
...productData,
images: productData.images.map((image) => {
if (image._id === i) return {...image} // modify here
return image
}),
});
Where newId
is your new _id, and newUrls
is your new urls其中newId
是您的新 _id, newUrls
是您的新网址
setProductData(prev => ({
...prev,
images: [...prev.images, {_id: newId, urls: newUrls}]
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.