javascriptreact-nativedatepickerandroid-datepicker

@react-native-community/datetimepicker shows popup-picker 2 times


I'm using @react-native-community/datetimepicker in my app. This is supposed to collect a user-defined date from the datepicker and then send it to the database. But, when I press a button and open the datepicker, the datepicker popup shows up two times. I pick a date, press ok and the window closes. And then only some ms after, a new date picker opens up. Anyone know why this happens?

const onChange = (event, selectedDate) => {
        var inputDate = selectedDate.toISOString()
        var outputDate = inputDate.split("T")[0]
        setSelectedDate(outputDate)
        setDate(date)
        setShow(false);
        console.log(outputDate)
    }

const showDatepicker = () => {
        setShow(true)
    }

...

<View>
   <Button title="Choose date" onPress={showDatepicker} />
      </View>
      {show ? (
         <DateTimePicker
            value={date}
            onChange={onChange}
            mode={'date'}
            display="default"
         />
      ) : (<View />)}

Solution

  • Try below code that might help

    const onChange = (event, selectedDate) => {
        const inputDate = selectedDate.toISOString();
        const outputDate = inputDate.split('T')[0];
        setSelectedDate(outputDate);
        setDate(outputDate);
      };
    
      React.useEffect(() => {
        setShow(false);
      }, [date]);