Skip to content Skip to sidebar Skip to footer

How To Call Alert Onpress And Pass Some Parameters In React Native

Here I am calling on alert function onPress on text field . On calling that function I am trying to open alert and on confirm I am calling another function. But it gets hang if I a

Solution 1:

Try to change :

<TouchableOpacity key={index} onPress={this.showAlert1(data)}>

to

<TouchableOpacity key={index} onPress={() =>this.showAlert1(data)}>

And

showAlert1 (code,name,version) {  
    // code
}

To

showAlert1 = (code,name,version) => {  
    // code
}

Solution 2:

Make Sure you have Imported "Alert" from 'react-native', not some other module.

https://i.stack.imgur.com/oMj8s.png

First of all, try changing this:

<SmallTexttext={'Proceed'}  onPress={this.showAlert1(data.offering.code,data.offering.version,data.offering.name)}textColor='white' />
to:

<SmallTexttext={'Proceed'}  onPress={() => this.showAlert1(data.offering.code,data.offering.version,data.offering.name)} textColor='white' />

Also try to change

showAlert1 (code,name,version) {  
    #code
}

to

showAlert1 = (code,name,version) => {  
    // code
}

Solution 3:

as the Kishan Bharda answer addition. when we met the problem, we should know why not just correct.

as for how to pass the function to the component props, you can read the official blog, and get more details

when we want to pass params to props, here are two ways:

<TouchableOpacity key={index} onPress={() => this.showAlert1(data)}>
<TouchableOpacity key={index} onPress={this.showAlert1.bind(this,data)}>

when your do like your question

<TouchableOpacity key={index} onPress={this.showAlert1(data)}>

it is not pass the funtion, it is called not a reference.

Post a Comment for "How To Call Alert Onpress And Pass Some Parameters In React Native"