How to trigger onPress event of TouchableOpacity?

4k views Asked by At

Using jQuery in html:

$("input:button").on("click",function(){alert(1)}).click()

It can trigger click event.

I want to do the similar thing to onPress event of TouchableOpacity in react-native.

<TouchableOpacity activeOpacity={0.75} key={id} onPress={() => {onChangeAttribute(key)}}>
    {<Text>{value.name}</Text>}
</TouchableOpacity>

I don't know how to do this. I google it but found nothing. I just want to trigger onPress event in code not by user's tapping.

Thanks in advance.

1

There are 1 answers

2
Vahid Boreiri On

You should make your code like this:

render () {
  <TouchableOpacity activeOpacity={0.75} key={id} onPress={onChangeAttribute(key)}>
    { <Text>{value.name}</Text> }
  </TouchableOpacity>
}