Dialog opens twice on Android
See original GitHub issueIf 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
Issue Analytics
- State:
- Created 4 years ago
- Reactions:86
- Comments:43 (1 by maintainers)
Top Results From Across the Web
How to prevent dialog to open twice - android - Stack Overflow
Take a count variable i.e., count=0; . In button click validate condition such that if(count==0) show dialog and make count to 1. (with...
Read more >Dialog repeating twice (Android forum at Coderanch)
I have a dialog in my activity which is inside the onFinish method of a CountDownTimer. So as soon as the timer finishes...
Read more >Prevent Opening Dialog Twice When Tapping A Button
Tips & Tricks Android Java. Prevent Opening Dialog Twice When Tapping A Button | Android Java. 104 views 2 years ago.
Read more >Thread: Dialog opens twice - Qt Centre Forum
If user clicked a button then a dialog will pop up. The problem is that the dialog pops up twice, one after another....
Read more >Android – How to prevent dialog to open twice - iTecNote
I had button in my app, on doubleClick of my button I open a dialog. Sometimes when I doubleClick the button in a...
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 FreeTop 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
Top GitHub Comments
I fixed this problem by closing the picker before handling it’s value. Swap
setFieldValue
andsetShowDatePicker
intoonChange
handlerHope this will work
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
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:
With this you no longer need to set the showDatePicker state onChange() so just this: