React-day-picker: How to reset value of daypicker?

Created on 29 Mar 2019  路  2Comments  路  Source: gpbl/react-day-picker

Hi. How to reset value of day picker ?

All 2 comments

Here is a possible solution using component state

const DateComponent = () => {
    const initialValue = new Date();
    const [currentDate, setCurrentDate] = useState( initialValue );

    function reset() {
        setCurrentDate( initialValue );
    }

    return (
        <DayPicker selectedDays={selectedDate} />
    );
};

You could also set key on react-day-picker component and change it whenever you need and it will reset internal state of component. You can read more about it here https://reactjs.org/blog/2018/06/07/you-probably-dont-need-derived-state.html#recommendation-fully-uncontrolled-component-with-a-key

Was this page helpful?
0 / 5 - 0 ratings