Javascript 日期选择器e.target.value未定义
我正在使用日历的Datepicker组件用用户选择的日期更新我的状态 当我试图使用输入-反应-输出错误更改默认日期时:“TypeError:e.target未定义” 多谢各位 声明:Javascript 日期选择器e.target.value未定义,javascript,reactjs,Javascript,Reactjs,我正在使用日历的Datepicker组件用用户选择的日期更新我的状态 当我试图使用输入-反应-输出错误更改默认日期时:“TypeError:e.target未定义” 多谢各位 声明: this.handleSubmit = this.handleSubmit.bind(this); this.handleDate = this.handleDate.bind(this); this.handleGuests = this.handleGuests.bind(this); this.handleN
this.handleSubmit = this.handleSubmit.bind(this);
this.handleDate = this.handleDate.bind(this);
this.handleGuests = this.handleGuests.bind(this);
this.handleName = this.handleName.bind(this);
this.state = {
min: new Date().getHours,
max: new Date(),
booking: {
date: new Date(),
people: 1,
name: 'John Doe',
},
}
handleDate功能:
handleDate(e){
const booking = {...this.state.booking}
booking.date = e.target.value;
this.setState({booking});
}
日期选择器:
<DatePicker
name="Datepicker"
className="form-control mx-auto form-control-lg p-3"
selected={this.state.booking.date}
onChange={this.handleDate}
showTimeSelect
timeFormat="HH:mm"
timeIntervals={60}
dateFormat="MMMM dd, yyyy h:mm aa"
timeCaption="Time"
placeholderText="Click and choose the date"
/>
来自自述文件日期选择器组件: 回调不使用事件,它已在handleChange函数的参数中包含date对象
handleChange(date) {
this.setState({
startDate: date
});
}
render() {
return (
<DatePicker
selected={this.state.startDate}
onChange={this.handleChange}
/>
);
}
handleChange(日期){
这是我的国家({
开始日期:日期
});
}
render(){
返回(
);
}
试试这个:handleDate(dateValue){console.log(dateValue)}从e.target.value改为e,非常好用,谢谢@维顿,我试过这个,它对我有效,但我不明白为什么。你能解释一下原因吗?