2017-09-01 159 views
0
class Someclass { 
    @observable waiting = false; 
    @observable error = null; 

    @action.bound 
    async authenticate({login, password}) { 
    try { 
     this.waiting = true; 
     const res = await myService.authenticate(login, password); 
    } catch (error) { 
     this.error = error; // Gives an error: Invariant Failed 
    } finally { 
     this.waiting = false; // Gives an error: Invariant Failed 

     // I've tried doing this too 
     // action(() => { 
     // this.waiting = false; 
     // }) 
    } 
    } 


} 

在上面的例子中改變從漁獲的值改變從try catch塊可觀察到的和最後塊提供了一個錯誤或警告不變失敗嚴格模式。什麼是正確的方法?不能在mobx嚴格模式

回答

0

async功能,我們必須換一個函數內部runInAction實用方法在strict模式變異@observable。例。

class Someclass { 
    @observable waiting = false; 
    @observable error = null; 

    @action.bound 
    async authenticate({login, password}) { 
    try { 
     this.waiting = true; 
     const res = await myService.authenticate(login, password); 
    } catch (error) { 
     runInAction(() => { 
     this.error = error; 
     }); 
    } finally { 
     runInAction(() => { 
     this.waiting = false; 
     }); 
    } 
    } 


} 

View this related issue on github