Using all props as payload for your ViewModel
This recipe helpful if you want to use all props as payload (this.payload) in your ViewModel.
What you will need:
ViewModelBaseor custom implementation ofViewModelinterfacewithViewModelHOC
What you will do:
- Pass props type into first generic type parameter in
ViewModelBaseclass - Configure
withViewModelHOC withgetPayloadfunction that returns all props as payload - Override type of output React component into fixed type
tsx
import { withViewModel } from 'mobx-view-model';
import { ViewModelBase } from 'mobx-view-model';
interface ComponentProps {
foo: number;
}
class YourVM extends ViewModelBase<ComponentProps> {
}
export const YourComponent = withViewModel(
YourViewModel,
({ model }) => {
return (
<div>{model.payload.foo}</div>
)
},
{
getPayload: (props) => props
}
) as unknown as ComponentType<ComponentProps>
...
<YourComponent foo={1} />