I am building an react app and want to save multiple photos to firestore to do that i want to take their url in imageurl: [] an array to save them in firestore.
const [data, setData] = useState({
name: "",
phone: "",
location: "",
imageurl:[],
})
here every data is saving and working fine name,phone,location but the problem is with array value imageurl[] they are not saving
i m saving urls like
setData((prev)=> {
return {
...prev, [data.imageurl] : [...data.imageurl , url]
}
})
})
it is not working.
i am new to react please guide me how can i solve this problem. thank you
3
Answers
Try with this:
You can then call
addImageUrl(url)
whenever you want to add a URL to theimageurl
array. For example, you might call it inside an event handler that handles image uploads.In the
setData
when creating the return object you have put the value as key, when you set the key as[data.imageurl]
. also don’t use the data variable insidesetData
, use theprev
variable supplied by the functiontry this code
You can tried like this also:
The code updates the imageurl property in the component’s state by creating a new state object that’s a copy of the previous state and appends a new URL to the imageurl array.