File tree Expand file tree Collapse file tree 2 files changed +43
-0
lines changed
Expand file tree Collapse file tree 2 files changed +43
-0
lines changed Original file line number Diff line number Diff line change @@ -168,6 +168,7 @@ export default defineDocsVitepressConfig(configs, {
168168 text : 'Recipes 📃' ,
169169 link : '/recipes/observer-wrap-all-view-components' ,
170170 items : [
171+ { text : 'All props as payload' , link : '/recipes/all-props-as-payload' } ,
171172 { text : 'Wrap in observer() all view components' , link : '/recipes/observer-wrap-all-view-components' } ,
172173 { text : 'Wrap view components in custom HOC' , link : '/recipes/wrap-view-components-in-custom-hoc' } ,
173174 { text : 'Integration with RootStore' , link : '/recipes/integration-with-root-store' } ,
Original file line number Diff line number Diff line change 1+ # Using all props as payload for your ` ViewModel `
2+
3+ This recipe helpful if you want to use all props as ` payload ` (` this.payload ` ) in your ` ViewModel ` .
4+
5+
6+ What you will need:
7+ - [ ` ViewModelBase ` ] ( /api/view-models/base-implementation ) or custom implementation of [ ` ViewModel ` ] ( /api/view-models/interface ) interface
8+ - [ ` withViewModel ` ] ( /react/api/with-view-model ) HOC
9+
10+ What you will do:
11+ - Pass props type into first generic type parameter in ` ViewModelBase ` class
12+ - Configure ` withViewModel ` HOC with ` getPayload ` function that returns all props as payload
13+ - Override type of output React component into fixed type
14+
15+ ``` tsx{10,22,24}
16+ import { withViewModel } from 'mobx-view-model';
17+ import { ViewModelBase } from 'mobx-view-model';
18+
19+ interface ComponentProps {
20+ foo: number;
21+ }
22+
23+ class YourVM extends ViewModelBase<ComponentProps> {
24+
25+ }
26+
27+ export const YourComponent = withViewModel(
28+ YourViewModel,
29+ ({ model }) => {
30+ return (
31+ <div>{model.payload.foo}</div>
32+ )
33+ },
34+ {
35+ getPayload: (props) => props
36+ }
37+ ) as unknown as ComponentType<ComponentProps>
38+
39+ ...
40+
41+ <YourComponent foo={1} />
42+ ```
You can’t perform that action at this time.
0 commit comments