Razmooo
Razmooo

Reputation: 506

How to cancel AlertIOS programmatically

Description

I have a component with Barcode scanner. After the barcode scanner returns a value, the AlertIOS with a prompt shows up, with Cancel and Save buttons. Now one option is to click Save button for sending the value to the server. For the other option, the client wishes to send data with volume down button or cancel with volume up button.

Problem

Now I already have set up confirmation with volume down button, but I would also like to close the Alert popup when pressing any of the buttons.

My code

For alert:

showAlert(value) {
     this.alert = AlertIOS.prompt(
        `Gescannter Wert: ${value}`,
        'Menge eingeben (Standartwert ist 1)',
        [{
            text: 'Cancel',
            onPress: () => this.scanning = true,
            style: 'cancel',
        },
        {
            text: 'Save',
            onPress: (input) => this.pushCodeData(value, input)
        }],
        'plain-text',
        '1',
        'number-pad',
    );
}

for volume change detection:

        this.volumeListener = SystemSetting.addVolumeListener((data) => {
        let volume = data.value.toFixed(1);
        console.log(volume, this.VOLUME);
        SystemSetting.setVolume(this.VOLUME);

        if (volume < this.VOLUME && this.scanning === false) {
            console.log("confirm")
            this.pushCodeData(this.state.scannedValue, 1)

        } else if (volume > this.VOLUME && this.scanning === false){
            console.log("cancel")
            this.scanning = true
        }
    });

The function that sends the data is this.pushCodeData(params...)

Is it even possible to programmatically close the alert prompt? So far I couldn't find any answers to this.

Upvotes: 0

Views: 98

Answers (1)

Aaditya Paliwal
Aaditya Paliwal

Reputation: 607

It's not possible to close an alert programmatically yet. https://github.com/facebook/react-native/issues/4928

You may give it a try.

How can I remove Alert prompts programmatically?

Upvotes: 1

Related Questions