javascript - 如何在 React Native 中调用 Alert onPress 并传递一些参数
问题描述
在这里,我在文本字段上调用警报功能 onPress 。在调用该函数时,我试图打开警报并确认我正在调用另一个函数。但是如果我调用 "showAlert1()" 就会挂起。这个函数被多次调用,我必须调用 showAlert() 函数 onPress 并且我必须在其中发送一些值。并在警报框上的确认 OK 按钮上,我必须上传到服务器。
showAlert1(code, name, version) {
console.log("data alaert abc", code, name, 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, name, version) },
]
);
}
confirmTariffMigration = (code, name, version) => {
console.log("hhhhdhhdhdhdhhdd", code, name, 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>
解决方案
尝试改变:
<TouchableOpacity key={index} onPress={this.showAlert1(data)}>
到
<TouchableOpacity key={index} onPress={() => this.showAlert1(data)}>
和
showAlert1 (code,name,version) {
// code
}
到
showAlert1 = (code,name,version) => {
// code
}
推荐阅读
- excel - 根据搜索词列表将文本从 Word 复制到 Excel
- android - 如何获取每个音频样本的时间
- c# - 使用 command.parameters() 的动态过滤器和列
- c - 我得到了这段代码来在列表末尾插入一个元素。分段错误
- azure - Azure 功能:访问存储帐户时出现 403 错误
- python - 信息提取 - htmls
- android - 以编程方式更改 DarkMode
- regex - 什么是匹配字符串的正则表达式,除了它有一个 $ 美元符号?
- r - 如何从 2 列中抽取 90/10 拆分% 的行
- reactjs - Redux-saga:处理仅在一个组件中使用的异步调用数据的最佳方式