Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call AlertIOS in React Native

I am an iOS developer, but I know a little javascript. I am trying to use the AlertIOS, the document api is this

static alert(title: string, message?: string, buttons?: Array<{ text: ?string; onPress: ?Function; }>) 

I am confused with the parameters. I tried to write like this,but it give me error. AlertIOS('Username empty', 'Please type your username', buttons: {{text: 'Cancel', onPress: onPressCancel}});

How can i use AlertIOS properly ?

like image 458
yong ho Avatar asked Sep 29 '22 10:09

yong ho


1 Answers

If you look at the documentation, it's says that there's an AlertIOS API with a static method called alert. That means you can call it like this:

AlertIOS.alert('Username empty', 'Please type your username', [{text: 'Cancel', onPress: onPressCancel}]);

Notice that you also don't need the "buttons:" prefix for the buttons array - that part of your call wasn't valid syntax anyway.

The method signature for alert is documented using Flow type annotations. Each argument is described like this:

  • name of argument: type of argument

And if the name has a question mark, that argument is optional. So, in this case the arguments are:

  • title, with a type of string
  • message, with a type of string (optional)
  • buttons, with a type of array (optional)

You'll also need to make sure you require the AlertIOS API, probably something like this:

var {
  AppRegistry,
  StyleSheet,
  View,
  AlertIOS
} = React;

Hope that helps.

like image 105
Colin Ramsay Avatar answered Oct 26 '22 21:10

Colin Ramsay