2017-02-15 19 views
0

に "this"が定義されていません。私はhandleClick(onClickハンドラ)を定義し、非同期関数を呼び出したいコンポーネントがあります。私がしたことの下のコード、私の質問:なぜ 'this'は非同期関数()呼び出しでアクセスできないのですか?メソッドの子非同期func

class GameListItem extends BaseComponent { 
constructor(props, context) { 
    super(props, context); 
    this.state = { 
     name: props.name, 
     owner: props.owner, 
     uid: props.uid, 
     status: props.status 
    }; 
    this.handleClick = this.handleClick.bind(this); 
} 

handleClick() { 
    let inst = this; 
    (
     async function() { 

      // WHY HERE 'this' is undefined? 

      let res = await inst.engine.async_action({'action': 'join_game', 'data': {'game_uid': inst.state.uid}}); 
      console.log('res: ', res); 
     }() 
    ); 
} 

render() { 
    return (
     <li className="list-group-item" > 
      <ProtectedLink text={this.state.name} classes="game_link" onClick={this.handleClick}/> 
     </li> 
    ); 
} 
} 
+0

この関数は 'this'を使用しません。 – zeroflagL

答えて

0

解決済み。私はIIFEで文脈を失った。これは機能します:

(
     async function(inst) { 
      let res = await inst.engine.async_action({'action': 'join_game', 'data': {'game_uid': inst.state.uid}}); 
      console.log('res: ', res); 
     } 
    )(this);