Dialog opens twice on Android
If i open the Datepicker it opens twice on Android. I select in the first Dialog a date. then the same dialog opens again and i have to select a date again . After the second time it will accept the input. Can someone help me?
Thats the code of the component. Most of the components are just for styling:
const DatePickerInput = ({
inputName,
locale,
labelKey,
max,
min,
}) => {
const { values, setFieldValue } = useFormikContext();
const [t] = useTranslation('validatedTextInput');
const [showDatePicker, setShowDatePicker] = useState(false);
const initialDate = values[inputName] || new Date();
const [selectedDate, setSelectedDate] = useState(moment(initialDate).toDate());
const datePlaceholderKey = 'datePlaceholder';
return (
<DatePickerContainer>
<DatePickerLabel>
{t(labelKey)}
</DatePickerLabel>
<DatePickerButtonContainer>
<DatePickerButton
onPress={() => setShowDatePicker(!showDatePicker)}
>
<DatePickerButtonText>
{selectedDate
? moment(selectedDate).format('L')
: t(datePlaceholderKey)}
</DatePickerButtonText>
<DatePickerButtonImage source={Calendar} />
</DatePickerButton>
</DatePickerButtonContainer>
{
showDatePicker && (
<DateTimePicker
mode="date"
display="spinner"
value={selectedDate}
onChange={(event, value) => {
setFieldValue(inputName, value);
setShowDatePicker(!showDatePicker);
// setSelectedDate(value);
}}
maximumDate={max}
minimumDate={min}
locale={locale}
/>
)
}
</DatePickerContainer>
);
};
Thx for your help
Did you resolve this problem @Niore? I'm having a similar issue :/
no still got no solution for it.
Have you tried setShowDatePicker(Platform.OS === 'ios' ? true : false);?
I remember having the same issue and I think the above was part of the solution.
The problem is because of the rerender queue when using useState hooks explained here https://stackoverflow.com/a/54120412
To implicitly order the rerenders and avoid a second datepicker just do
onChange={(event, value) => {
setShowDatePicker(Platform.OS === 'ios'); // first state update hides datetimepicker
setFieldValue(inputName, value);
setSelectedDate(value);
}}
To be more explicit I suggest the useStateWithCallback hook created by @rwieruch here https://github.com/the-road-to-learn-react/use-state-with-callback/blob/master/src/index.js
And replace your current selectedDate state hook with the following:
const [selectedDate, setSelectedDate] = useStateWithCallback(
initialDate,
() => setShowDatePicker(Platform.OS === 'ios'),
);
With this you no longer need to set the showDatePicker state onChange() so just this:
onChange={(event, value) => {
setFieldValue(inputName, value);
setSelectedDate(value);
}}
I fixed this problem by closing the picker before handling it's value.
Swap setFieldValue and setShowDatePicker into onChange handler
onChange={(event, value) => {
setShowDatePicker(!showDatePicker);
setFieldValue(inputName, value);
// setSelectedDate(value);
}}
Hope this will work
@vovka-s I've been scratching my head all day on this one, thank you!
Another solution is just creating a memoized wrapper for const MemoizedDateTimePicker = React.memo((props) => <DateTimePicker {...props} />)
I fixed it with the following code:
const [state, setState] = useState({
date: new Date(),
mode: 'date',
show: false
});
const onChange = (event, selectedDate) => {
const currentDate = selectedDate || state.date;
setState({...state, date: currentDate, show: false});
};
const showPicker = currentMode => {
setState({...state, show: true});
};
{ state.show &&
(<DateTimePicker
testID="dateTimePicker"
timeZoneOffsetInMinutes={0}
value={state.date}
mode={state.mode}
is24Hour={true}
display="default"
onChange={onChange}
/>)
}
The problem is because of the rerender queue when using useState hooks explained here https://stackoverflow.com/a/54120412
To implicitly order the rerenders and avoid a second datepicker just do
onChange={(event, value) => { setShowDatePicker(Platform.OS === 'ios'); // first state update hides datetimepicker setFieldValue(inputName, value); setSelectedDate(value); }}To be more explicit I suggest the useStateWithCallback hook created by @rwieruch here https://github.com/the-road-to-learn-react/use-state-with-callback/blob/master/src/index.js
And replace your current selectedDate state hook with the following:
const [selectedDate, setSelectedDate] = useStateWithCallback( initialDate, () => setShowDatePicker(Platform.OS === 'ios'), );With this you no longer need to set the showDatePicker state onChange() so just this:
onChange={(event, value) => { setFieldValue(inputName, value); setSelectedDate(value); }}
Works like a charm. Thanks!
For anyone who the above solution didn't work for, I was having an identical issue. Spent 3 days trying to figure it out and eventually realised it was caused by the debugger. Stopping debug fixed the issue for me.
try that
const handleConfirm = (datetime) => {
hideDatePicker() //must be first
console.log("A date has been picked: ", datetime)
setChosenDate(moment(datetime).format('dddd Do MMMM YYYY à HH:mm'))
}
If not, try to use onChange callback instead of onConfirm
It works for me
const handleConfirm = (datetime) => { hideDatePicker() //must be first console.log("A date has been picked: ", datetime)
setChosenDate(moment(datetime).format('dddd Do MMMM YYYY à HH:mm')) }
Thank you very much ! It works !
try that
const handleConfirm = (datetime) => { hideDatePicker() //must be first console.log("A date has been picked: ", datetime) setChosenDate(moment(datetime).format('dddd Do MMMM YYYY à HH:mm')) }If not, try to use
onChangecallback instead ofonConfirmIt works for me
@ vovka-s Thank you very much . It worked for me, you saved my day.
For anyone who the above solution didn't work for, I was having an identical issue. Spent 3 days trying to figure it out and eventually realised it was caused by the debugger. Stopping debug fixed the issue for me.
This kind of solved it for me
For anyone who the above solution didn't work for, I was having an identical issue. Spent 3 days trying to figure it out and eventually realised it was caused by the debugger. Stopping debug fixed the issue for me.
wow, that helped me as well, I spent about 4 hours trying to figure out what's wrong with the picker!
The problem is because of the rerender queue when using useState hooks explained here https://stackoverflow.com/a/54120412
To implicitly order the rerenders and avoid a second datepicker just do
onChange={(event, value) => { setShowDatePicker(Platform.OS === 'ios'); // first state update hides datetimepicker setFieldValue(inputName, value); setSelectedDate(value); }}To be more explicit I suggest the useStateWithCallback hook created by @rwieruch here https://github.com/the-road-to-learn-react/use-state-with-callback/blob/master/src/index.js
And replace your current selectedDate state hook with the following:
const [selectedDate, setSelectedDate] = useStateWithCallback( initialDate, () => setShowDatePicker(Platform.OS === 'ios'), );With this you no longer need to set the showDatePicker state onChange() so just this:
onChange={(event, value) => { setFieldValue(inputName, value); setSelectedDate(value); }}
Thank you that solved for me. I have it set this way if anybody needs a Modal component that supports both OS. DatePickerModal
I tried all the above, and none helped me. But using
onHide={() => setShowDatePicker(false)}
worked like a charm :)
Hi! for me work this example when you have two DateTimePicker in same page.
const showTimepicker = (event: any) => {
event.preventDefault()
setShow(true);
};
<TextLinkButton onPress={(event) => showTimepicker(event)} label={label} />
I hope it works for you. A greeting!
I tried all the above, and none helped me. But using
onHide={() => setShowDatePicker(false)}worked like a charm :)
react-native-datetimepicker has no onHide method
I fixed this problem by closing the picker before handling it's value. Swap
setFieldValueandsetShowDatePickerintoonChangehandleronChange={(event, value) => { setShowDatePicker(!showDatePicker); setFieldValue(inputName, value); // setSelectedDate(value); }}Hope this will work
working !! Tada !!