Notice that
Please note that this library currently exposes functionality from UIDatePicker on iOS and DatePickerDialog + TimePickerDialog on Android, and CalendarDatePicker +TimePicker on Windows.
These native classes offer only limited configuration, while there are dozens of possible options you as a developer may need. It follows that if your requirement is not supported by the backing native views, this library will not be able to implement your requirement. When you open an issue with a feature request, please document if (or how) the feature can be implemented using the aforementioned native views. If those views do not support what you need, such feature requests will be closed as not actionable.
as provided in the react-native-datetimepicker documentation.
The date
prop that you are using is one of them. For iOS it exposes a field of UIDatePicker which is
The initial date that the date picker will display. Defaults to the current date, but you can set a custom value. This attribute is equivalent to setting the date property programmatically.
However, as stated in the react-native-datetimepicker
documentation its usage is limited. The correct prop that we need to use is the value
prop which
Defines the date or time value used in the component.
This field is required. We cannot unset it. There will always be some default value which needs to be explicitly set initially.
However, what you want to achieve can be done differently. You need to handle the form value yourself in a state of the form. Thus,
const [date, setDate] = useState()
The state date
in your form is initially undefined. The value
prop of your DateTimePicker
is not.
The DateTimePicker
gets an onConfirmMethod
. If that is fired set your date
state to the value
prop passed to the onConfirmMethod
as follows
const onConfirm = (selectedDate) => {
setDate(selectedDate)
}
<DateTimePicker
isVisible={isDatePickerVisible}
mode={mode}
onConfirm={onConfirm}
onCancel={hideDatePicker}
value={date}
onBlur={onBlur}
/>
}
Usually you want the user to allow to delete the field afterwards. You need to implement this yourself, since value
cannot be unset. If the selected date
is visualized in some kind of InputField, then this can easily be done.
The data of your form is then decoupled from your DateTimePicker
value prop. I'm afraid that there is no other way, at least with this library.