2017-07-19 17 views
1

現在、子コンポーネントを通じてコン​​ポーネントの状態を更新しようとしていますが、コールバック関数は状態が更新されていないことを通知します。

方法(loadModuleContent)状態変化の状態が異常に変化しました

export default class LayoutPage extends React.Component { 
    constructor() { 
     super(); 
     this.startSession = this.startSession.bind(this); 
     this.loadModuleContent = this.loadModuleContent.bind(this); 
     this.state = { 
      content: undefined, 
      group: undefined, 
      title: undefined, 
      selectionIndex: undefined 
     } 
    } 

    static navigationOptions = { 
     header: null 
    }; 

    loadModuleContent(_content, _title, _selectedIndex) { 
     console.log("Inserting index: " + _selectedIndex); 
     this.setState({ 
      content: _content, 
      title: _title, 
      selectionIndex: _selectedIndex 
     }, console.log("State updated:" + this.state.selectionIndex)); 
    } 
... 

コンソールログ
enter image description here

答えて

2

setStateコールに意図したようにあなたはcallbackを使用していません。コールバックを渡す代わりに、すぐに評価されるメソッド呼び出しを渡しています。あなたは第二のparamで機能を書き出すあなたが持っているES06を使用しない場合は、あなたのsetStateこの

this.setState({ 
     content: _content, 
     title: _title, 
     selectionIndex: _selectedIndex 
    },() => console.log("State updated:" + this.state.selectionIndex)); 

に変更します。

+0

とても愚かな感じ....ありがとう –

関連する問題