Skip to content

Commit ab0196e

Browse files
committed
docs: add recipe for all props as payload
1 parent 3ab3d67 commit ab0196e

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

docs/.vitepress/config.mts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff 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' },
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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+
```

0 commit comments

Comments
 (0)