Instruction file imported from Sandipan006/lokalmind-app (
.cursor/rules/mobx.mdc). Copyright stays with the author.
MobX Rules
Full reference: docs/development/CONVENTIONS.md section 4.
ViewModel template
import { makeAutoObservable, runInAction } from 'mobx';
export class ExampleViewModel {
isLoading = false;
errorMessage: string | null = null;
constructor(private readonly someUseCase: SomeUseCase) {
makeAutoObservable(this); // always first
}
async doAction() {
this.isLoading = true;
const result = await this.someUseCase.execute();
runInAction(() => {
this.isLoading = false;
if (!result.success) this.errorMessage = result.error.message;
});
}
get isReady() { return !this.isLoading; }
}
Hard rules
makeAutoObservable(this)- first statement in constructor, always- Async state mutations - always in
runInAction() - Computed - getter syntax only, no
@computed - All screens/components reading VM state - must be wrapped with
observer() - MobX only in
presentation/- never indomain/,data/,infrastructure/ - No
useState/useReducerfor state that belongs in a ViewModel