question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Android: DateTimePicker keeps resetting to default value

See original GitHub issue

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.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:18
  • Comments:17 (2 by maintainers)

github_iconTop GitHub Comments

12reactions
PaulOskarSoecommented, Jun 12, 2020

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])}
3reactions
vonovakcommented, Jan 2, 2021

🎉 This issue has been resolved in version 3.0.9 🎉

The release is available on:

Your semantic-release bot 📦🚀

Read more comments on GitHub >

github_iconTop Results From Across the Web

Datepicker returning to initial value - react native
I'm using react-native-date-picker component to pick date and time values in my app. In Android it's working fine.
Read more >
My fields keep resetting to the default values in power apps
Solved: Hi All In power apps I have a Edit form that users capture case related information they go and select date when...
Read more >
DevicePolicyManager
In order to protect any sensitive data that remains on the device, it is advised that the device owner factory resets the device...
Read more >
react-native-community/datetimepicker
Defines the type of the picker. List of possible values: "date" (default for iOS and Android and Windows ); "time" ...
Read more >
Element: <oj-input-date>
When the reset method is called, deferred validation is run after all ... Default values for the datePicker sub-properties can also be ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found