i'm working on a project with React and Firestore. I'm working on a page which will read through multiple images and certain fields of these images will be changed. Once changes are made, i just want the one image doc to update rather than everything on the page hence onSnapshot. The onSnapshot listener keeps adding on to the images that i have whenever changes are made. I understand that everytime useEffect is called it adds the same images but i don't know how to fix it. I have seen some solves here but they don't cater to the IDs array which i need to reference a document when making changes to it.
export default function Images({ user }) {
const [images, setImages] = useState([]);
const [ids, setIDs] = useState([]);
useEffect(() => {
const colRef = collection(db, "images");
const q = query(colRef, where("user", "==", user));
onSnapshot(q, (snapshot) => {
snapshot.docs.forEach((doc) => {
setImages((prev) => [...prev, doc.data()]);
setIDs((prev) => [...prev, doc.id]);
});
});
}, [user]);
return (
<>
{images && (
<div className="m-4 grid grid-cols-2 grid-flow-row">
{images.map((image, index) => {
const imageID = ids[index];
return <Labels image={image} imageID={imageID} />;
})}
</div>
)}
</>
);
}
Any insights will be appreciated.