Simple usage
The most simplest way to make integration with this library is to use ViewModelSimple
interface
Steps:
- Implement
ViewModelSimple
interface
tsx
import { ViewModelSimple } from 'mobx-view-model';
import { makeAutoObservable } from 'mobx';
export class MyPageVM implements ViewModelSimple {
state = '';
constructor() {
makeAutoObservable(this);
}
setState = (state: string) => {
this.state = state
}
}
- Create instance of your ViewModel using
useCreateViewModel()
hook
tsx
import { observer } from 'mobx-react-lite';
import { ViewModelPayload, useCreateViewModel } from 'mobx-view-model';
const MyPageView = observer(() => {
const model = useCreateViewModel(MyPageVM);
return <div>{model.state}</div>;
});
- Use it
tsx
<MyPage />
If you need access more lifecycle methods or full ViewModel interface
This guide you can found on the next page