I am currently trying to make an app with React Native but I am facing a problem that i don’t understand.
I want to get the id of the element the user touched, I try to do this using a onPress in a
tasks.map(element => {
return (
<TouchableOpacity onPress={(element) => getTouchedId(element)}>
<Note text={element.text} color={element.color} setpositionX={setpositionX} setpositionY={setpositionY}/>
</TouchableOpacity>
)
})}
So onPress call getTouchedId and pass element as a parameter.
Here is my getTouchedId function :
const getTouchedId = (element) => {
console.log("element.id : " + element.id)
settouchedId(element.id)
}
So here you can see my function, I dont understand why I am never seeing my console.log("element.id : " + element.id)
I think the problem is that my function is never red and I don’t understand why (the value of touchedId
with settouchedId
also never changes).
3
Answers
Change
onPress={(element) => getTouchedId(element)}
toonPress={() => getTouchedId(element)}
.The reason is
element
hereonPress={(element)
is representing the event originating onpressing
but the not the element from the arrayDEMO
Are you sure onPress function has element parameter?
onPress defined like
PressEvent defined like
Just try this.
I hope it will be helpful.