MK12
MK12

Reputation: 491

Reset the current date in datepicker of ANTD on modal reopen in react js

Problem

After closing the modal the datepicker of antd not reset If user select the date 2029/Feb/02 and close the modal after reopen the modal in datepicker selected year is 2029

What I want

After reopen the modal the today date would be selected

What I tried

According to doc Antd if defaultPickerValue is set then it should show the current date on modal reopen, but it's not working

defaultPickerValue={moment()}

Code

Working example

Upvotes: 0

Views: 1970

Answers (1)

Harshil Laheri
Harshil Laheri

Reputation: 151

You need to set the initial state value to undefined instead of "". defaultValue will only set the value if the value prop is actually undefined.

EDIT: You also need to add destroyOnClose on the Modal. defaultValue is only set once when the component is mounted and since closing the modal is not actually unmounting the component, it will retain its state value.

https://codesandbox.io/s/fix-date-picker-inside-modal-window-in-react-js-forked-hcclx6?file=/index.js:707-716

Upvotes: 1

Related Questions