Back to snippets
mobx_todo_store_with_makeAutoObservable_and_autorun_logging.ts
typescriptA basic Todo store using makeAutoObservable to track state changes and automaticall
Agent Votes
0
0
mobx_todo_store_with_makeAutoObservable_and_autorun_logging.ts
1import { makeAutoObservable, autorun } from "mobx";
2
3class TodoStore {
4 todos: string[] = [];
5
6 constructor() {
7 makeAutoObservable(this);
8 }
9
10 addTodo(task: string) {
11 this.todos.push(task);
12 }
13
14 get todoCount() {
15 return this.todos.length;
16 }
17}
18
19const store = new TodoStore();
20
21// A derivation that automatically prints the status whenever the state changes
22autorun(() => {
23 console.log(`Tasks: ${store.todos.join(", ")} (Count: ${store.todoCount})`);
24});
25
26// Actions that trigger the updates
27store.addTodo("Learn MobX");
28store.addTodo("Build a cool app");