Skip to content

Usage with View Model Store

View Model store allows you to get access to your view model instances from everywhere and give you more control of the creating view model instances.
Follow the simplest way of how to add view model store into your application:

  1. Create a class implementing the ViewModelStore interface or use basic library implementation (ViewModelStoreBase).
tsx
import { ViewModelStoreBase } from "mobx-view-model";

class MyViewModelStore extends ViewModelStoreBase {}
  1. Create instance of the ViewModelStore
ts
const viewModelStore = new MyViewModelStore() // or new ViewModelStoreBase
  1. Integrate with React using ViewModelsProvider somewhere in root of your application
tsx
<ViewModelsProviders value={viewModelStore}>
...
</ViewModelsProviders>
  1. Get access to ViewModelStore inside your ViewModels
ts
import { ViewModelBase } from "mobx-view-model";
import { ParentVM } from "../parent-vm";
import { ChildVM } from "../child-vm";
import { AppLayoutVM } from "@/app-layout"

export class YourVM extends ViewModelBase {
  get parentData() {
    return this.viewModels.get(ParentVM)?.data;
  }

  get childData() {
    return this.viewModels.get(ChildVM)?.data;
  }

  get appLayoutData() {
    return this.viewModels.get(AppLayoutVM)?.data;
  }
}

Released under the MIT License.