我有两个异步调用,如下所示。我尝试将axios调用包装在this.setState
的回调中,但是会引发类似, expected
this.setState(prevState => ({
condition: [...prevState.condition, { name: "readByGroup", operator: "IN ", value: groups }]
}))
}
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"})
})
我尝试的内容如下:
this.setState(prevState => ({
condition: [...prevState.condition, { name: "readByGroup", operator: "IN ", value: groups }]
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"})
})
}))
答案 0 :(得分:1)
我看不到您在this.setState中运行回调函数的位置。
尝试:
this.setState(prevState => ({
condition: [...prevState.condition, { name: "readByGroup", operator: "IN ", value: groups }]
}), () => {
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"})
}))
}
键是箭头,它是setState的下一个参数。