[Android] Datepicker closes only the second time
See original GitHub issuereact-native-date-picker
version is 4.1.1
.
Describe the bug After selecting a date and clicking on “Confirm” the modal window closes and opens again. You must click “Confirm” again to close it permanently.
In this video I open datepicker and then click “Confirm”:

To Reproduce
export const App = () => {
const [showDatePicker, setShowDatePicker] = useState(false);
const [birthdate, setBirthdate] = useState(new Date());
return (
<>
<Button title="Open" onPress={() => setShowDatePicker(true)} />
<DatePicker
modal
open={showDatePicker}
date={birthdate}
mode="date"
onConfirm={(date) => {
setBirthdate(date);
setShowDatePicker(false);
}}
onCancel={() => {
setShowDatePicker(false);
}}
/>
</>
);
};
I thinks this is happening because two setState
are called in one time inside onConfirm
handler. This bug is solved with wrapping setState
calls in unstable_batchedUpdates
:
import { unstable_batchedUpdates } from 'react-native';
onConfirm={(date) => {
unstable_batchedUpdates(() => {
setBirthdate(date);
setShowDatePicker(false);
});
}}
Smartphone (please complete the following information):
- OS: Android
react-native info
:
System:
OS: macOS 12.0.1
CPU: (12) x64 Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz
Memory: 36.68 MB / 16.00 GB
Shell: 5.8 - /bin/zsh
Binaries:
Node: 14.17.0 - /usr/local/bin/node
Yarn: 1.22.10 - /usr/local/bin/yarn
npm: 6.14.13 - /usr/local/bin/npm
Watchman: 2021.09.13.00 - /usr/local/bin/watchman
Managers:
CocoaPods: 1.11.2 - /usr/local/bin/pod
SDKs:
iOS SDK:
Platforms: DriverKit 21.0.1, iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0
Android SDK: Not Found
IDEs:
Android Studio: 2020.3 AI-203.7717.56.2031.7678000
Xcode: 13.1/13A1030d - /usr/bin/xcodebuild
Languages:
Java: javac 17 - /usr/bin/javac
npmPackages:
@react-native-community/cli: Not Found
react: 17.0.2 => 17.0.2
react-native: 0.66.0 => 0.66.0
react-native-macos: Not Found
npmGlobalPackages:
*react-native*: Not Found
Issue Analytics
- State:
- Created 2 years ago
- Comments:8 (3 by maintainers)
Top Results From Across the Web
DatePicker.OnDateChangedListener called twice
I'm trying to create an app where the user selects a date from a DatePicker, and then a list is updated with some...
Read more >Date picker component — Vuetify
v-date-picker is a fully featured date selection component that lets users select a date, or range of dates. # Usage. Date pickers come...
Read more >Android Date Time Picker Dialog | DigitalOcean
Android Date Time picker are used a lot in android apps. In this tutorial we'll demonstrate the use of a Date Picker and...
Read more >Date Picker Dialog Example | APG | WAI - W3C
Choosing a date from the calendar closes the dialog and populates the date input field. When the dialog is opened, if the input...
Read more >DatePicker Tutorial With Example In Android Studio
For selecting time Android also provides timepicker to select time. ... Example 2: In the second example we show the use of date...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I definitely agree! Also, the order shouldn’t really matter so I’ll keep this issue open until it’s fixed.
Hi, thanks for reporting this. I am aware of this issue that happens when the set function is called before the show(false) function. Try change the order of the two and let me know if it solved it for you!