React Native-以编程方式关闭警报

时间:2019-01-28 22:50:42

标签: javascript reactjs react-native alert

我在React Native中有一个TextInput AlertIOS

 AlertIOS.prompt(
  'Enter password',
  'Enter your password to claim your $1.5B in lottery winnings',
  [
    {
      text: 'Cancel',
      onPress: () => console.log('Cancel Pressed'),
      style: 'cancel',
    },
    {
      text: 'OK',
      onPress: (password) => password.trim() != "" ? Dismiss ALERT : KEEP ALERT,
    },
  ],
  'secure-text',
);

有没有办法仅以编程方式关闭警报?即当单击非取消选项时,我只想在条件满足时关闭警报。

这可能吗?

1 个答案:

答案 0 :(得分:-1)

如果您想在点击Cancel按钮时解除警报,则可以使用像onPress: () => {}这样的空函数代替console.log()。 如果您希望在条件满足时解除警报,可以尝试以下操作:

onPress: (password) => password.trim() !== "" ? this.doSomething() : this.doAnotherStuff();

onPress: (password) => password.trim() !== "" ? this.doSomething() : () => {};