私のアプリケーションでは、非同期を同期に変換する必要があります(つまり、setStateが値を設定したら、呼び出し後にapiからデータをフェッチする必要があります)
logChange(val) {
this.setState({
fetchIntentReport: {
startDate: this.state.fetchIntentReport.startDate,
endDate: this.state.fetchIntentReport.endDate,
intents: val.split(','),
},
});
this.props.fetchIntentReports({
startDate: this.state.fetchIntentReport.startDate,
endDate: this.state.fetchIntentReport.endDate,
intents: this.state.fetchIntentReport.intents,
});
}
値がインテントに設定されたら、reduxを介してfetchIntentReportsapi呼び出しを呼び出す必要があります。
同期呼び出しを強制しないことを強くお勧めします。幸い、setState
コールバック関数が許可されているため、次のことができます。
logChange(val) {
var startDate = this.state.fetchIntentReport.startDate;
var endDate = this.state.fetchIntentReport.endDate;
var intents = val.split(',');
this.setState({
fetchIntentReport: {
startDate,
endDate,
intents
}
}, () => {
// if you need the updated state value, use this.state in this callback
// note: make sure you use arrow function to maintain "this" context
this.props.fetchIntentReports({
startDate,
endDate,
intents
})
);
}
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加