The react state value this.state.condition
is getting passed to axios
call , but this.state.condition
is mutated only on the second attempt of execution of function generateReport()
, so that this.state.condition
is passed as an empty array to an axios call on first attempt of the execution of the function generateReport()
. Is there any work-around or solution to the problem?
The code is given below.
generateReport(){
this.setState({statusMsg: ""});
this.setState({loaderInitialStatus:"Processing..."})
//this.isReq() ?
console.log('this.state.selectedOption???',this.state.selectedOption);
if(this.state.selectedOption && this.state.selectedOption.length > 0) {
let groups = []
this.state.selectedOption.map((item) => {
groups.push(item.value);
})
this.setState(prevState => ({
condition: [...prevState.condition, { name: "readByGroup", operator: "IN ", value: groups }]
}))
}
console.log('this.state.condition???', this.state.condition);
this.props.getMetricsByContent(this.state.condition).then((data) => {
this.setState({isLoader: false});
if(data && Array.isArray(data) && data.length > 0){
let csvContent = papa.unparse(data);
this.download(csvContent, 'metrics.csv', 'text/csv;encoding:utf-8');
this.setState({statusMsg: "File Downloaded successfully"})
} else this.setState({statusMsg: "No records to download"})
})
//: null;
}