Setting a time and date on Android is much different from iOS. With iOS, you have a DatePickerIOS
component that includes both the date and time. On Android, this is split into two native modals, DatePickerAndroid
for the date and TimePickerAndroid
for the time. It's not a component to render either, it's an asynchronous function that opens the modal and waits for a natural conclusion before applying logic to it.
To open one of these, wrap an asynchronous function around it:
async renderDatePicker () { const { action, year, month, day } = await DatePickerAndroid.open({ date: new Date() }); if (action === DatePickerAndroid.dismissedAction) { return; } // do something with the year, month, and day here }
Both the DatePickerAndroid
and TimePickerAndroid
components return an object, and we can grab the properties of each object by using ES6 destructuring assignment, as shown in the preceding snippet.
As these components will render...