'If I was mapping thru a collection, and placing buttons for each doc, how could I grab the docID from the button to its function?
How could one grab the documentID of the Button from a mapped dataset, where each item is an object?
function doSomethingWithDocID = () => {
firestore.collection("MainESGfeed")
.doc(documentID)
.update({likes: increment })
console.log("liked!")
}
return(
{data.map((itemObject, pos) => {
const {likes, comments, documentID} = itemObject
<div>
{likes}
{comments}
<Button onClick={doSomethingWithDocID}/>
</div>
)
}
})
Solution 1:[1]
You can use an arrow function, and supply documentID when you call doSomethingWithDocID:
return data.map((documentID, pos) => (
<div>
<Button key={documentID} onClick={() => doSomethingWithDocID(documentID)}/>
</div>
)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Ori Drori |
