I have a date picker which I have to pass month and year to API from this picker. After extraction of month and year, i have to call a function where API will call.
Before taking the month and date, API is calling. I tried to handle by using .then but of no use.
Please help me handle this
My code as follows: Here this.getWindTurbineData(); function is calling before passing the date
componentDidMount() {
this.getWindTurbineData();
}
getWindTurbineData = () => {
let month = this.state.mm;
let year = this.state.yyyy;
let getTurbineUrl = `url_year?month=${month}&year=${year}`;
axios.get(getTurbineUrl).then((response) => {
let res = response.data;
});
this.setState({ turbineData: res });
});
};
handleDate = (day) => {
var mmyy = new Date(day);
mmyy = JSON.stringify(mmyy);
let mm = mmyy.slice(6, 8);
let yyyy = mmyy.slice(1, 5);
this.setState({ mm, yyyy }, () => {
console.log(this.state.yyyy, this.state.mm);
});
this.getWindTurbineData();
/*
this.setState({ mm, yyyy }, () => {
console.log(this.state.yyyy, this.state.mm);
}).then(()=>{ this.getWindTurbineData(); });
*/
};