2017-09-01 3 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; 
     // }) 
    } 
    } 


} 

In obigem Beispiel beobachtbaren ändern Werte von dem Fang zu verändern und schließlich gibt Block einen Fehler oder eine Warnung unveränderliche fehlgeschlagen mit striktem Modus. Was ist der richtige Weg?kann nicht von Try-Catch-Block in mobx Strict-Modus

Antwort

0

Innerhalb von async Funktion müssen wir eine Funktion innerhalb runInAction Dienstprogramm Methode wickeln für @observable in strict Modus mutiert. Beispiel.

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