Reputation:
There is a problem with a React component. Maybe someone khow, what's wrong here. I use the react-datepicker library in my project to fill the date input on the client side. All form inputs work, but an error occurs when I try to select a date in the calendar drop-down menu.
I'm new to react, and maybe there is some kind of error in working with the states. I would be grateful for any help!
Here is the listing:
import React, { Component } from 'react';
import axios from 'axios';
import DatePicker from "react-datepicker";
import "react-datepicker/dist/react-datepicker.css";
class Transfer extends Component {
constructor(props) {
super(props);
this.state = {
value: [],
loading: true,
error: false,
startDate: new Date()
};
this.handleSubmit = this.handleSubmit.bind(this);
}
onChange(e) {
this.setState({ [e.target.name]: e.target.value })
}
handleSubmit(e) {
e.preventDefault();
const data = {
name: this.state.name,
email: this.state.email,
phone: this.state.phone,
people: this.state.people,
date: this.state.date,
text: this.state.text
};
axios.post('http://localhost:5555/email/transfer', { data })
.then(res => {
console.log(data);
})
}
render() {
return (
<div className="wrapper">
<h5>Заказать трансфер</h5>
<form action="" onSubmit={this.handleSubmit}>
<div className="label-section">Имя</div>
<input name="name" id="name" type="text" defaultValue="" onChange={e => this.onChange(e)} /><br />
<div className="label-section">Email</div>
<input name="email" id="email" type="text" defaultValue="" onChange={e => this.onChange(e)} /><br />
<div className="label-section">Телефон</div>
<input name="phone" id="phone" type="text" defaultValue="" onChange={e => this.onChange(e)} /><br />
<div className="label-section">Количество человек</div>
<input name="people" id="people" type="number" min="1" max="11" defaultValue="" onChange={e => this.onChange(e)} />
<div className="label-section">Дата</div>
<DatePicker
id="date"
name="date"
defaultValue=""
selected={this.state.startDate}
onChange={e => this.onChange(e)}
/>
<div className="label-section">Оставьте нам сообщение</div>
<textarea name="text" id="text" type="text" cols={30} rows={10} defaultValue="" onChange={e => this.onChange(e)} /><br /><br />
<button type="submit" className="xbutton">Отправить</button>
</form>
</div>
);
}
}
export default Transfer;
Thanks for any help!)
Upvotes: 2
Views: 5823
Reputation: 5747
React-datepicker doesn't fire off the same kind of event that a regular input field would
The react-datepicker onChange function returns a single value - the date selected.
An html input onChange function returns a change event
Upvotes: 2
Reputation:
OK, I changed something and it worked. Not sure that I did it in a better way, but it works.
I declare another function:
onChangeDate(date) {
console.log(date.toISOString());
this.setState({ startDate: date })
}
And use it for the Datepicker component:
<DatePicker
id="date"
name="date"
defaultValue=""
selected={this.state.startDate}
onChange={date => this.onChangeDate(date)}
/>
And here, in the array:
const data = {
name: this.state.name,
email: this.state.email,
phone: this.state.phone,
people: this.state.people,
date: this.state.startDate,
text: this.state.text
};
Upvotes: 2