What I'm trying:
- Take two date input (Start Date & End Date) at once on a single click of a button.
- By default their values should be the of today's date ie if one input is left untouched (and one selected) and then button clicked, values should be user selected and by-default today's date respectively.
- Output should be in order yyyy/mm/dd
- What should be the most React way to approach this?
My approach:
import React, { Component } from 'react';
import '../App.css';
class DateSelector extends Component {
constructor(props){
super(props);
this.state = {
startDate:"yyyy-MM-dd",
endDate:"yyyy-MM-dd"
}
}
dateHandler = (event) => {
console.log("startDate: "+this.state.startDate+" endDate:"+this.state.startDate);
}
startDateOnChange = (event) => {
this.setState({startDate: event.target.value});
}
endDateOnChange = (event) => {
this.setState({endDate: event.target.value});
}
render() {
return (
<div>
START DATE<input type="date" id="startDate" value="{this.state.startDate}" onChange={this.startDateOnChange}/>
END DATE<input type="date" id="endDate" value="{this.state.startDate}" onChange={this.endDateOnChange}/>
<button type="submit" onClick={this.dateHandler}>Submit</button>
</div>
);
}
}
export default DateSelector;
The above code gives following error on Date tag's onChange
event:
The specified value "{this.state.startDate}" does not conform to the required format, "yyyy-MM-dd".
And <button>
tag's onClick
gives empty string as startDate: endDate:
NOTE: I am quite new to web designing thing. I did quite googling but didn't find any related tutorial/doc/post. Any guide/reference (for future reference) regarding these kind of situations is much appreciated.