Skip to content

Instantly share code, notes, and snippets.

@Odonno
Created June 1, 2020 09:32
Show Gist options
  • Save Odonno/34be13fa63f1285034b13508d985da74 to your computer and use it in GitHub Desktop.
Save Odonno/34be13fa63f1285034b13508d985da74 to your computer and use it in GitHub Desktop.
State management comparison - mobx - Models
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