Created
June 1, 2020 09:32
-
-
Save Odonno/34be13fa63f1285034b13508d985da74 to your computer and use it in GitHub Desktop.
State management comparison - mobx - Models
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export class TodoList { | |
@observable.shallow list: Todo[] = []; | |
constructor() { | |
autorun(() => { | |
this.load(); | |
}); | |
} | |
@action | |
async load() { | |
const [err, getTodosResponse] = await usePromise( | |
fetch(`${apiUrl}/todos`) | |
); | |
if (err || !getTodosResponse) { | |
return; | |
} | |
const [err2, todosJson] = await usePromise( | |
getTodosResponse.json() | |
); | |
if (err2 || !todosJson) { | |
return; | |
} | |
const todos = todosJson.map((t: any) => new Todo(t.id, t.content)); | |
runInAction(() => { | |
this.list = todos; | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment