您可以使用onChange
带有两个参数的 HandlerdateName
和dateValue
//in state
this.state = {
startDate: moment(),
endDate: moment(),
}
//My onChange handler function
handleDateChange(dateName, dateValue) {
let { startDate, endDate } = this.state;
if (dateName === 'startDateTime') {
startDate = dateValue;
} else {
endDate = dateValue;
}
this.setState({
startDate,
endDate,
});
}
// Date Picker component
<DatePicker
id="start-date-time"
name="startDateTime"
className="form-control"
selected={this.state.startDate}
value={this.state.startDate}
onChange={date => this.handleDateChange('startDateTime', date)}
showTimeSelect
timeFormat="HH:mm"
timeIntervals={15}
dateFormat="YYYY-MM-DD, h:mm a"
timeCaption="time"
/>
//other use of Date picker
<DatePicker
id="end-date-time"
name="endDateTime"
className="form-control"
selected={this.state.endDate}
value={this.state.endDate}
onChange={date => this.handleDateChange('endDateTime', date)}
placeholderText="choose end date of event"
isClearable={true}
showTimeSelect
timeFormat="HH:mm"
timeIntervals={15}
dateFormat="YYYY-MM-DD, h:mm a"
timeCaption="time"
/>