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

33,970

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.
Share:
33,970

Related videos on Youtube

boy_v
Author by

boy_v

Updated on January 10, 2020

Comments

  • boy_v
    boy_v over 4 years

    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


  • boy_v
    boy_v almost 7 years
    Many Thanks . for this method :)
  • Satyam
    Satyam over 3 years
    How can I pass a parameter to the function "onDeleteBTN" ?