Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I call method from onPress on Alert function [React-Native]

Tags:

How can I call method from onPress on Alert function [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> 

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}}, 

It's not work


like image 327
boy_v Avatar asked Jun 13 '17 16:06

boy_v


People also ask

How do you call a function on button click in React Native?

Example: Call a Function After Clicking a Buttonimport React from 'react'; function App() { function sayHello() { alert('Hello!' ); } return ( <button onClick={sayHello}> Click me!

How do you call a component onPress in React Native?

The onPress event is called when user touch or press the Text component. To Set onPress onClick on Text in React Native we have to use onPress={} prop in react native. The onPress event is supported by both Android and iOS platforms.

How do you use alert onPress in React Native?

Tapping any button will fire the respective onPress callback and dismiss the alert. By default, the only button will be an 'OK' button. This is an API that works both on Android and iOS and can show static alerts. Alert that prompts the user to enter some information is available on iOS only.

How do you call a function in onPress?

Pressing the button will call the "onPress" function, which in this case displays an alert popup. If you like, you can specify a "color" prop to change the color of your button.


1 Answers

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>   ); } 

Note:

  • 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.
like image 145
Michael Cheng Avatar answered Sep 21 '22 04:09

Michael Cheng