e头调用Alert onPress并在React Native中发送其中的一些参数

在这里,我在文本字段上调用alart函数onPress。 在调用该功能时,我试图打开警报,并在确认时在调用其他功能。 但是如果我打电话给“ showAlert1()”,它就会死机。此功能多次被调用 我必须在showPress上调用showAlert()函数,并且必须在中发送一些值。然后在Alert的确认OK按钮上,我必须上传到服务器。

showAlert1(code,name,version) {
  console.log("data alaert abc",code,version);
  Alert.alert(
    'Confirmation','Are you sure you want to migrate this tariff',[
      {
        text: 'Cancel',onPress: () => console.log('Cancel pressed'),style: 'Cancel',},{ text: 'Proceed',onPress: () => this.confirmTariffMigration(code,version) },]
  );
}

confirmTariffMigration = (code,version) => {
  console.log("hhhhdhhdhdhdhhdd",version);
  const objData = {
    addofferingactionCode: '',offeringCode: '',offeringName: ''
  }
  this.props.updatetariffMigration(objData)
}
<View style={{ marginLeft: 5,marginRight: 5,marginTop: 10,backgroundColor: '#f1f1f1' }}>
  {
    tariffMigrationData.map((data,index) => {
      return (
        //  <TouchableOpacity key={index} onPress={this.showAlert1(data)}>
        <View style={{ marginBottom: 10,marginLeft: 5,marginRight: 5 }} key={index}>
          <Card>
            <CardItem header style={{ backgroundColor: '#fff',width: '100%',justifyContent: 'space-between',borderBottomColor: '#f1f1f1',borderBottomWidth: 1 }}>
              <View style={{ flexDirection: 'column',justifyContent: 'space-between' }}>
                <View>
                  <RegularText text={`${data.offering.name}`} style={{ fontWeight: 'bold' }} />
                  <SmallText text={` ID ${data.offering.code}`} textColor="grey" />
                </View>
              </View>
              <View style={{
                backgroundColor: 'blue',borderRadius: 75,height: 25,paddingRight: 10,paddingLeft: 10,paddingTop: 5
              }}>
                <SmallText text={'Proceed'} onPress={this.showAlert1(data.offering.code,data.offering.version,data.offering.name)} textColor='white' />
              </View>
            </CardItem>
          </Card>
        </View>
      )
    }
  }
</View>
holylai 回答:e头调用Alert onPress并在React Native中发送其中的一些参数

尝试更改:

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

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

还有

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

收件人

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

确保您从“ react-native”(而非其他模块)导入了“ Alert”。

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

首先,尝试更改此内容:

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

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

也尝试更改

showAlert1 (code,version) {  
    #code
}

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

,

作为Kishan Bharda答案的补充。当我们遇到问题时,我们应该知道为什么不正确。

有关如何将功能传递给组件道具的信息,您可以阅读官方的blog,并获取更多详细信息

当我们要将参数传递给道具时,有两种方法:

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

当您喜欢您的问题时

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

它没有通过功能,称为非引用。

本文链接:https://www.f2er.com/3154999.html

大家都在问