Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
class ListStore<T> {
isLoading = false;
list: T[] = [];
constructor(loadList: () => Promise<T[]>) {
makeAutoObservable(this)
}
loadList() {
this.isLoading = true;
loadList()
.then(action((data) => this.list = data))
.finally(action(() => this.isLoading = false))
}
}
const userListStore = new ListStore<User>(() => api.loadUsers());
const postListStore = new ListStore<Post>(() => api.loadPosts());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment