Reputation: 13
How to get the selected date inside the onSelect function, after update the selected date in the state and
onSelect = (cal) => {
this.setState({ selectedValue: cal });
alert(this.state.selectedValue);
I got the previous date in the alert but i need the current selected date, how to achieve this...?
Upvotes: 1
Views: 1353
Reputation: 1567
From the antd docs, DatePicker
uses an onChange
event, not onSelect
.
This should log the correct value.
<DatePicker
format="MM/DD/YYYY"
placeholder="Banana"
locale="en_US"
onChange={console.log}
/>
Upvotes: 0
Reputation: 191
Problem here is that this.setState({})
is asynchronous. That means that alert can happen before state gets updated. What you can do is pass a callback.
Check this example which logs state after it has been updated:
this.setState({ selectedValue: cal }, () =>
console.log(this.state.selectedValue);
);
Upvotes: 2