2017-07-11 3 views
1

を反応させるのにSETSTATEの作り方、私がSETSTATE私はポストコールには私のアプリケーションでは、同期プロセス

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, 
    }); 
} 

一度のAPIからデータをフェッチする必要があり、値を設定したら(IE)を同期する非同期に変換する必要があります値がインテントに設定されている場合、私はredux経由でfetchIntentReports APIコールを呼び出す必要があります。

+0

[SETSTATE](https://facebook.github.io/react/docs/react-component.html#setstate)メソッドがコールバックを受け入れます第2のパラメータとして使用する。それは本当に同期させる方法はありません。ドキュメントで説明されているように、それはもっと要求です。 – Leandro

答えて

2

同期呼び出しを強制することを強くお勧めします。あなたは次の操作を行うことができますので幸い、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 
     }) 
    ); 
} 
+1

yaその仕事はすばらしい@Dom –

+0

@KARTHISRV素晴らしい!あなたが何か他のものを必要としたら教えてください! – Dom

関連する問題