如何在Alert函数上调用onPress中的方法[React-Native] [英] How can I call method from onPress on Alert function [React-Native]

查看:409
本文介绍了如何在Alert函数上调用onPress中的方法[React-Native]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<Button
  onPress={{() => Alert.alert(
    'Alert Title',
    'alertMessage',
    [
      {text: 'Cancel', onPress: () => console.log('Cancel Pressed!')},
      {text: 'OK', onPress: () => {this.onDeleteBTN}},

    ],
    { cancelable: false }
  )}}
  >
      <Text> Delete Record </Text>
</Button>






警报对话框上的OK按钮
我需要致电


After OK button on Alert Dialog I need to call

onDeleteBTN = () => {
    alert(' OnDelete');
}







{text: 'OK', onPress: () => {this.onDeleteBTN.bind(this)}},
{text: 'OK', onPress: () => {this.onDeleteBTN}},






它不起作用

推荐答案

第一期, Button 组件有一个 title prop,而不是< Text> as一个孩子。第二个问题是你有一堆语法错误,并没有正确调用函数(或绑定)。如果你修复它,那它应该工作正常;例如:

First issue, the Button component has a title prop instead of having <Text> as a child. Second issue is that you have a bunch of syntax errors and are not calling functions (or binding) correctly. If you fix that, then it should work fine; for example:

alert = (msg) => {
  console.log(msg)
}

onDeleteBTN = () => {
  this.alert(' OnDelete')
}

render() {
  return (
    <View style={styles.container}>
      <Button
        title="Delete Record"
        onPress={() => Alert.alert(
          'Alert Title',
          'alertMessage',
          [
            {text: 'Cancel', onPress: () => console.log('Cancel Pressed!')},
            {text: 'OK', onPress: this.onDeleteBTN},
          ],
          { cancelable: false }
        )}
      />
    </View>
  );
}

注意:


  • 我不知道你的 alert()函数应该做什么,所以我做了一个假的记录到控制台。

  • 还有其他方法可以调用 onDeleteBTN()或绑定。

  • I don't know what your alert() function is supposed to do, so I made a dummy one that logs to console.
  • There are other ways of doing this like calling onDeleteBTN() or binding.

这篇关于如何在Alert函数上调用onPress中的方法[React-Native]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆