如何在React Native中调用Alert onPress并传递一些参数
How to call Alert onPress and pass some parameters in React Native
我在这里调用文本字段上的警报功能 onPress 。
在调用该函数时,我试图打开警报并确认我正在调用另一个函数。
但是如果我调用 "showAlert1()" 它会挂起。这个函数被调用了很多次
我必须调用 showAlert() 函数 onPress 并且我必须在其中发送一些值。在警告框上确认确定按钮后,我必须上传到服务器。
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
}
确保您从 '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.version,data.offering.name)} textColor='white' />
也尝试改变
showAlert1 (code,name,version) {
#code
}
到
showAlert1 = (code,name,version) => {
// code
}
作为 Kishan Bharda 的补充回答。当我们遇到问题时,我们应该知道为什么不纠正。
关于如何将函数传递给组件props,可以阅读官方blog,了解更多详情
当我们想给 props 传递参数时,有两种方式:
<TouchableOpacity key={index} onPress={() => this.showAlert1(data)}>
<TouchableOpacity key={index} onPress={this.showAlert1.bind(this,data)}>
当您喜欢您的问题时
<TouchableOpacity key={index} onPress={this.showAlert1(data)}>
不是传递函数,调用不是引用
我在这里调用文本字段上的警报功能 onPress 。 在调用该函数时,我试图打开警报并确认我正在调用另一个函数。 但是如果我调用 "showAlert1()" 它会挂起。这个函数被调用了很多次 我必须调用 showAlert() 函数 onPress 并且我必须在其中发送一些值。在警告框上确认确定按钮后,我必须上传到服务器。
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
}
确保您从 '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.version,data.offering.name)} textColor='white' />
也尝试改变
showAlert1 (code,name,version) {
#code
}
到
showAlert1 = (code,name,version) => {
// code
}
作为 Kishan Bharda 的补充回答。当我们遇到问题时,我们应该知道为什么不纠正。
关于如何将函数传递给组件props,可以阅读官方blog,了解更多详情
当我们想给 props 传递参数时,有两种方式:
<TouchableOpacity key={index} onPress={() => this.showAlert1(data)}>
<TouchableOpacity key={index} onPress={this.showAlert1.bind(this,data)}>
当您喜欢您的问题时
<TouchableOpacity key={index} onPress={this.showAlert1(data)}>
不是传递函数,调用不是引用