I'm trying to store the date value from input to state but state shows undefined. The date value is not storing in state. Console gives as Undefined help me to solve the problem
<React components has a built-in state object. The state object is where you store property values that belongs to the component. When the state object changes, the component re-renders.>
import "./admin.css"
import Axios from "axios"
class Report extends React.Component {
state={
append1:[]
}
date =(e,name)=>{
if (name === "from") {
let ND = new Date(e.target.value);
ND.setMonth(ND.getMonth());
var dt = new Date(ND);
let da = dt.getDate();
if (da < 10) {
da = "0" + da;
}
let mth = dt.getMonth() + 1;
if (mth < 10) {
mth = "0" + mth;
}
let yr = dt.getFullYear();
var ED = yr + "-" + mth + "-" + da;
console.log(ED)
this.setState({from : ED});
console.log(this.state.from)
}
if (name === "to") {
let ND = new Date(e.target.value);
ND.setMonth(ND.getMonth());
var dt = new Date(ND);
let da = dt.getDate();
if (da < 10) {
da = "0" + da;
}
let mth = dt.getMonth() + 1;
if (mth < 10) {
mth = "0" + mth;
}
let yr = dt.getFullYear();
var ED1 = yr + "-" + mth + "-" + da;
this.setState({
to : ED1
});
console.log(this.state.to)
}
}
onsubmit(){
console.log(this.state.from)
// console.log(this.state.to)
// let fromto={
// // from : this.state.from,
// to: this.state.to
// }
// Axios.post("http://localhost:4000/bsdate",fromto).then((response)=>{
// console.log(response)
// this.setState({append1: response.data.data})
// })
}
render(){
return(
<div><center>
<form>
Report
From <input type="date" onChange={e=>this.date(e,"from")}
value={this.state.from}/>
<br/>
To <input type="date" onChange={e=>this.date(e,"to")}
value={this.state.to}
/>
<br/>
<button type="button" onClick={this.onsubmit}>Submit</button>
</form>
{this.state.append1}
</center>
</div>
)
}
}
export default Report;```