datetimepicker icon indicating copy to clipboard operation
datetimepicker copied to clipboard

Android: DateTimePicker keeps resetting to default value

Open philippe-ftx opened this issue 6 years ago • 19 comments

My DateTimePicker keeps resetting to the default value while open.

Code:

const CustomDateTimePicker = React.memo((onChange) => {

    console.log('Rerender CustomDateTimePicker');

    const [date, setDate] = useState(new Date('1985/01/01'));
    const [show, setShow] = useState(Platform.OS === 'ios');

    const onDateChange = useCallback((event, date) => {
        setShow(Platform.OS === 'ios');
        setDate(date);
        onChange(date);
    }, []);

    useEffect(() => {
        console.log('Date changed');
    }, [date]);

    return (
        <View style={{ flexDirection: 'row' }}>
            {Platform.OS === 'android' &&
                <TouchableOpacity
                    onPress={() => setShow(true)}
                    style={{
                        flex: 1,
                        marginVertical: 10,
                        padding: 10,
                    }}
                >
                    <Text style={{ fontSize: 16 }}>{date && date.toLocaleDateString()}</Text>
                </TouchableOpacity>
            }
            {show &&
                <DateTimePicker
                    testID="dateTimePicker"
                    textColor={'#ffffff'}
                    value={date}
                    mode={'date'}
                    display='default'
                    onChange={onDateChange}
                />
            }
        </View>
    )
});

export default CustomDateTimePicker;

When the dialog is open and I select a date, the component will undo my change and go back to the default value after a second or two.

I don't the console prints while this is happening.

philippe-ftx avatar May 25 '20 14:05 philippe-ftx

Any update on this ?

podplatnikm avatar Jun 01 '20 19:06 podplatnikm

hello, does this happen on ios or android?

vonovak avatar Jun 01 '20 20:06 vonovak

hello, I don't know for ios but it happens on android

nicolasmsg avatar Jun 03 '20 11:06 nicolasmsg

can confirm this still happening for Android

obiwankenoobi avatar Jun 08 '20 14:06 obiwankenoobi

I had a similar issue. I think the problem comes from the re-rendering of the calendar component. Try wrapping your parent component inside useMemo hook and render the component only if show calendar state changes. It worked for me :)

        {/* ONLY RENDER CALENDAR WHEN SHOW CALENDAR CHANGES */}
        {React.useMemo(() => {
          return (
            <CalendarDatesPicker
              pickDate={pickDate}
              selectedDate={selectedDate}
              language={language}
              closeCalendar={() => setCalendar(false)}
              showCalendar={showCalendar}
            />
          );
        }, [showCalendar])}

PaulOskarSoe avatar Jun 12 '20 08:06 PaulOskarSoe

Same issue here.

the-codepunker avatar Jun 19 '20 13:06 the-codepunker

Did you try wrapping parent component into useMemo?

PaulOskarSoe avatar Jun 19 '20 15:06 PaulOskarSoe

Did you try wrapping parent component into useMemo?

I did and it worked by wrapping it in useMemo... however, I suppose this is just a temporary fix...

the-codepunker avatar Jun 22 '20 13:06 the-codepunker

Did you try wrapping parent component into useMemo?

I did and it worked by wrapping it in useMemo... however, I suppose this is just a temporary fix...

In my case, I had a polling function that triggered re-render every 5-8 seconds, which caused the resetting of the value. With useMemo I was able to tell react don't re-render calendar component and give new props. You can also check your re-rendering, I think that might cause the issue with the resetting to the default value.

PaulOskarSoe avatar Jun 22 '20 14:06 PaulOskarSoe

as I understood, we save our data after press ok for android, and if we select some date, after ±5 sek value moves to default. I wrap my component into useMemo

evgenyshenets91 avatar Jul 29 '20 08:07 evgenyshenets91

Why can we fix it if we don't use Hooks ?

lokomass avatar Sep 18 '20 07:09 lokomass

@lokomass Yes, you just have to prevent re-rendering. I think shouldComponentUpdate will do the work. https://reactjs.org/docs/react-component.html

PaulOskarSoe avatar Sep 18 '20 10:09 PaulOskarSoe

Finally, this save my life :) https://github.com/react-native-community/datetimepicker/issues/68#issuecomment-639776038

lokomass avatar Sep 18 '20 12:09 lokomass

This work : https://github.com/react-native-community/datetimepicker/issues/68#issuecomment-639776038 But when I use minimumDate and maximumDate, the default value is always resetting... I can't change picker value

lokomass avatar Sep 29 '20 12:09 lokomass

:tada: This issue has been resolved in version 3.0.9 :tada:

The release is available on:

Your semantic-release bot :package::rocket:

vonovak avatar Jan 02 '21 22:01 vonovak

I tried this workaround. Works fine.

const DTPicker = ({ mode, onChange, value, placeholder, label,}) => {

    const [show, setShow] = useState(false);
    
    const handleDate = (e) => {
        if (e.nativeEvent.timestamp) onChange(e.nativeEvent.timestamp)
        setShow(Platform.OS === 'ios')
    }
    
    return <View>
        <TextInput placeHolder={placeholder} label={label} onFocus={() => setShow(true)} value={value} />
        {React.useMemo(() => {
            return show && <DateTimePicker
                value={new Date()}
                mode={mode}
                display="default"
                onChange={handleDate}
            />
        }, [show])}
    </View>
};

kapilislive avatar Jan 22 '21 06:01 kapilislive

I'm still getting this issue on 3.5.2

pixelknitter avatar Jan 26 '22 01:01 pixelknitter

@pixelknitter Is this still an issue on v7?

simonsolnes avatar Mar 11 '24 12:03 simonsolnes

I tried this workaround. Works fine.

const DTPicker = ({ mode, onChange, value, placeholder, label,}) => {

    const [show, setShow] = useState(false);
    
    const handleDate = (e) => {
        if (e.nativeEvent.timestamp) onChange(e.nativeEvent.timestamp)
        setShow(Platform.OS === 'ios')
    }
    
    return <View>
        <TextInput placeHolder={placeholder} label={label} onFocus={() => setShow(true)} value={value} />
        {React.useMemo(() => {
            return show && <DateTimePicker
                value={new Date()}
                mode={mode}
                display="default"
                onChange={handleDate}
            />
        }, [show])}
    </View>
};

i use this solution and working (8.4.5)

Anamcoollzz avatar Dec 16 '25 18:12 Anamcoollzz