React-native-modal-datetime-picker: Showing alert then dismissing modal will also dismiss the alert on iOS

Created on 28 Dec 2020  路  5Comments  路  Source: mmazzarolo/react-native-modal-datetime-picker

Environment

Platforms

iOS only

Versions

Description

I want to show an alert to the user after they have selected a date.

const handleConfirm = (date) => { alert("A Date has been Picked!"); hideDatePicker(); };

The problem on iOS is that hideDatePicker() will also dismiss the new alert, therefore it'll flash briefly on the screen

Here's a video

https://user-images.githubusercontent.com/3513340/103245957-e718c300-49b5-11eb-85d3-670ecc9b5190.mov

Reproducible Demo

Here is a snack demonstrating the issue - please run on iOS

And if that doesn't work here's the code

```import React, { useState } from "react";
import { Button, View, Alert } from "react-native";
import DateTimePickerModal from "react-native-modal-datetime-picker";

const DatePickerModalExample = () => {
const [isDatePickerVisible, setDatePickerVisibility] = useState(false);

const showDatePicker = () => {
setDatePickerVisibility(true);
};

const hideDatePicker = () => {
setDatePickerVisibility(false);
};

const handleConfirm = (date) => {
alert("A Date has been Picked!");
hideDatePicker();
};

return (

bug help wanted

Most helpful comment

@mmazzarolo thanks for your time on this, much appreciated! Yeah I think timeout is the way forward for now. Happy new year!

All 5 comments

Thanks @cgathergood :) Gonna check it tomorrow hopefully.
Wondering if this is happening because the callback is being invoked before setting the state...

Awesome thank you so much @mmazzarolo.
I've tried calling the alert after the state change but the outcome is the same. I can actually get it to work if I put in a timeout and play with the number of ms but that feels pretty dirty and I know device render speeds vary greatly.

Let me know if I can provide anything else :)

@cgathergood I did some tests and unfortunately I'm not able to solve the issue without the timeout, even in the onHide callback. The issue seems to actually be related only to the Alert API. In fact, if you try to re-show the modal in the onHide callback, the new modal shows up correctly (while the Alert doesn't).
I'd like to try the onDismiss callback but unfortunately it's not working in the latests versions of RN (it's still happening for me, even if the issue is closed...).
I think your best bet for now is using something like this:

const handleHide = () => {
  setTimeout(() => Alert.alert("Hello"), 0);
};

as your onHide callback.

I know, it sucks :/ and I'm open to suggestions on how to fix this directly on the component side.

https://github.com/mmazzarolo/react-native-modal-datetime-picker/blob/master/README.md#i-cant-show-up-an-alert-after-the-picker-has-been-hidden-on-ios

@mmazzarolo thanks for your time on this, much appreciated! Yeah I think timeout is the way forward for now. Happy new year!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

yash-atreya picture yash-atreya  路  3Comments

crobinson42 picture crobinson42  路  6Comments

DiegoDevBittencourt picture DiegoDevBittencourt  路  3Comments

ponleukea picture ponleukea  路  7Comments

taylorkline picture taylorkline  路  4Comments