Skip to content

Commit f403bcd

Browse files
committed
docs: update docs for modern approach of withViewModel HOC
1 parent 5003e17 commit f403bcd

File tree

8 files changed

+57
-56
lines changed

8 files changed

+57
-56
lines changed

docs/.vitepress/theme/style.css

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,4 +161,12 @@ html.dark {
161161
}
162162
p:has(>em:first-child:last-child) + div[class*=language-] {
163163
margin-top: -10px;
164+
}
165+
166+
.vp-doc strong a {
167+
font-weight: bold;
168+
}
169+
170+
.vp-doc strong code {
171+
font-weight: bold;
164172
}

docs/introduction/getting-started.md

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ The `mobx-view-model` source code is written on TypeScript and compiled into Nod
44

55
## Requirements
66

7-
- [`MobX`](https://mobx.js.org) **6+**
7+
- [`MobX`](https://mobx.js.org) **^6**
8+
- [`React`](https://reactjs.org) **^18** optional
89

910
## Installation
1011

@@ -14,14 +15,14 @@ The `mobx-view-model` source code is written on TypeScript and compiled into Nod
1415
npm install {packageJson.name}
1516
```
1617

17-
```bash [yarn]
18-
yarn add {packageJson.name}
19-
```
20-
2118
```bash [pnpm]
2219
pnpm add {packageJson.name}
2320
```
2421

22+
```bash [yarn]
23+
yarn add {packageJson.name}
24+
```
25+
2526
:::
2627

2728
## Writing first ViewModels
@@ -48,8 +49,7 @@ import { observer } from "mobx-react-lite";
4849
import { withViewModel, ViewModelProps } from "mobx-view-model";
4950
import { PetCardVM } from "./model";
5051

51-
const PetCardView = observer(({ model }: ViewModelProps<PetCardVM>) => {
52-
52+
export const PetCard = withViewModel(PetCardVM, ({ model }) => {
5353
return (
5454
<div className="p-10 flex flex-col gap-3">
5555
<span>{`Pet name: ${model.petName}`}</span>
@@ -64,8 +64,6 @@ const PetCardView = observer(({ model }: ViewModelProps<PetCardVM>) => {
6464
)
6565
})
6666

67-
export const PetCard = withViewModel(PetCardVM)(PetCardView)
68-
6967
...
7068
<PetCard />
7169
```

docs/introduction/usage/detailed-configuration.md

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ This way can be helpful when:
88

99
Follow the steps:
1010

11-
1. Make your own `ViewModel` implementation and interface with some customizations:
11+
##### 1. Make your own `ViewModel` implementation and interface with some customizations:
1212

1313
```ts{9,10}
1414
// view-model.ts
@@ -48,7 +48,7 @@ export class ViewModelImpl<
4848
```
4949

5050

51-
1. Make your own `ViewModelStore` implementation
51+
##### 2. Make your own `ViewModelStore` implementation
5252

5353
```ts{8,19,20,21}
5454
// view-model.store.impl.ts
@@ -81,7 +81,7 @@ export class ViewModelStoreImpl extends ViewModelStoreBase {
8181
}
8282
```
8383

84-
3. Create `View` with `ViewModel`
84+
##### 3. Create `View` with `ViewModel`
8585

8686
```tsx{2,4,10}
8787
import { ViewModelProps, withViewModel } from 'mobx-view-model';
@@ -106,11 +106,9 @@ export class MyPageVM extends ViewModelImpl {
106106
}
107107
}
108108
109-
const MyPageView = observer(({ model }: ViewModelProps<MyPageVM>) => {
109+
export const MyPage = withViewModel(MyPageVM, ({ model }) => {
110110
return <div>{model.state}</div>;
111111
});
112-
113-
export const MyPage = withViewModel(MyPageVM)(MyPageView);
114112
```
115113

116114
Also you may be helpful to read [**this recipe about integration with `RootStore`**](/recipes/integration-with-root-store)

docs/introduction/usage/simple.md

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,16 @@
22

33
The most simplest way to make integration with this library is to use [`ViewModelSimple` interface](/api/view-models/view-model-simple)
44

5-
Steps:
5+
Follow the steps:
66

7-
1. Implement [`ViewModelSimple` interface](/api/view-models/view-model-simple)
7+
##### 1. Create class or implement [`ViewModelSimple` interface](/api/view-models/view-model-simple)
88

99
```tsx
1010
import { ViewModelSimple } from 'mobx-view-model';
1111
import { makeAutoObservable } from 'mobx';
1212

13-
export class MyPageVM implements ViewModelSimple {
13+
// export class MyPageVM implements ViewModelSimple {
14+
export class MyPageVM {
1415
state = '';
1516

1617
constructor() {
@@ -23,20 +24,18 @@ export class MyPageVM implements ViewModelSimple {
2324
}
2425
```
2526

26-
2. Create instance of your ViewModel using [`useCreateViewModel()` hook](/react/api/use-create-view-model)
27+
##### 2. Create instance of your `ViewModel` using [`withViewModel()` HOC](/react/api/with-view-model)
2728

2829
```tsx
2930
import { observer } from 'mobx-react-lite';
3031
import { ViewModelPayload, useCreateViewModel } from 'mobx-view-model';
3132

32-
const MyPageView = observer(() => {
33-
const model = useCreateViewModel(MyPageVM);
34-
33+
const MyPage = withViewModel(MyPageVM, ({ model }) => {
3534
return <div>{model.state}</div>;
3635
});
3736
```
3837

39-
3. Use it
38+
##### 3. Use it
4039

4140
```tsx
4241
<MyPage />

docs/introduction/usage/with-base-implementation.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
Another simplest usage is to work with [base implementation](/api/view-models/base-implementation) of the [`ViewModel` interface](/api/view-models/interface)
44

5-
Steps:
5+
Follow the steps:
66

7-
1. Create your [`ViewModel`](/api/view-models/overview) class using [`ViewModelBase`](/api/view-models/base-implementation) (base implementation of [`ViewModel` package interface](/api/view-models/interface))
7+
##### **1.** Create your [`ViewModel`](/api/view-models/overview) class using [`ViewModelBase`](/api/view-models/base-implementation) (base implementation of [`ViewModel` package interface](/api/view-models/interface))
88

99
```tsx
1010
import {
@@ -31,17 +31,15 @@ export class MyPageVM extends ViewModelBase<{ payloadA: string }> {
3131
}
3232
```
3333

34-
2. Create view component using [HOC `withViewModel()`](/react/api/with-view-model)
34+
##### **2.** Create view component using [HOC `withViewModel()`](/react/api/with-view-model)
3535

3636
```tsx
3737
import { observer } from 'mobx-react-lite';
3838
import { withViewModel } from 'mobx-view-model';
3939

40-
const MyPageView = observer(({ model }: ViewModelProps<MyPageVM>) => {
40+
export const MyPage = withViewModel(MyPageVM, ({ model }) => {
4141
return <div>{model.state}</div>;
4242
});
43-
44-
export const MyPage = withViewModel(MyPageVM, MyPageView);
4543
```
4644

4745
or you can use [`useCreateViewModel()` hook](/react/api/use-create-view-model)
@@ -50,16 +48,18 @@ or you can use [`useCreateViewModel()` hook](/react/api/use-create-view-model)
5048
import { observer } from 'mobx-react-lite';
5149
import { ViewModelPayload, useCreateViewModel } from 'mobx-view-model';
5250

53-
const MyPageView = observer(
51+
export const MyPage = observer(
5452
({ payload }: { payload: ViewModelPayload<MyPageVM> }) => {
5553
const model = useCreateViewModel(MyPageVM, payload);
5654

5755
return <div>{model.state}</div>;
5856
},
5957
);
60-
```
58+
```
59+
::: tip don't forget to use the [`observer()` hoc](https://mobx.js.org/react-integration.html#react-integration)
60+
:::
6161

62-
3. Use it
62+
##### **3.** Use it
6363

6464
```tsx
6565
<MyPage payload={{ payloadA: '1' }} />

docs/introduction/usage/with-view-model-store.md

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,29 @@
33
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.
44
Follow the simplest way of how to add view model store into your application:
55

6-
1. Create a class implementing the [ViewModelStore interface](/api/view-model-store/interface) or use [basic library implementation (ViewModelStoreBase)](/api/view-model-store/base-implementation).
6+
##### **1.** Create a class implementing the [ViewModelStore interface](/api/view-model-store/interface) or use [basic library implementation (ViewModelStoreBase)](/api/view-model-store/base-implementation).
77

88
```tsx title="/src/shared/lib/mobx/view-model-store.ts"
99
import { ViewModelStoreBase } from "mobx-view-model";
1010

1111
class MyViewModelStore extends ViewModelStoreBase {}
1212
```
1313

14-
2. Create instance of the [ViewModelStore](/api/view-model-store/overview)
14+
##### **2.** Create instance of the [ViewModelStore](/api/view-model-store/overview)
1515

1616
```ts
1717
const viewModelStore = new MyViewModelStore() // or new ViewModelStoreBase
1818
```
1919

20-
3. Integrate with [React](https://react.dev/) using [`ViewModelsProvider`](/react/api/view-models-provider) somewhere in root of your application
20+
##### **3.** Integrate with [React](https://react.dev/) using [`ViewModelsProvider`](/react/api/view-models-provider) somewhere in root of your application
2121

2222
```tsx
23-
<ViewModelsProviders value={viewModelStore}>
23+
<ViewModelsProvider value={viewModelStore}>
2424
...
25-
</ViewModelsProviders>
25+
</ViewModelsProvider>
2626
```
2727

28-
4. Get access to `ViewModelStore` inside your `ViewModels`
28+
##### **4.** Get access to `ViewModelStore` inside your `ViewModels`
2929

3030
```ts
3131
import { ViewModelBase } from "mobx-view-model";
@@ -50,4 +50,3 @@ export class YourVM extends ViewModelBase {
5050

5151

5252

53-

docs/other/vite-plugin.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@ This library has a `Vite` plugin that **can** improve DX when using the library.
1010
npm install mobx-view-model-vite-plugin
1111
```
1212

13-
```bash [yarn]
14-
yarn add mobx-view-model-vite-plugin
15-
```
16-
1713
```bash [pnpm]
1814
pnpm add mobx-view-model-vite-plugin
1915
```
2016

17+
```bash [yarn]
18+
yarn add mobx-view-model-vite-plugin
19+
```
20+
2121
:::
2222

2323

docs/react/integration.md

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ To achieve this you can use:
1818

1919
## 2. Render in React tree
2020

21-
#### If you are using [withViewModel() HOC](/react/api/with-view-model)
21+
#### use [withViewModel() HOC](/react/api/with-view-model)
2222
Then you should render component returned from this function
2323

2424
```tsx
@@ -27,15 +27,16 @@ import { observer } from "mobx-react-lite";
2727

2828
class YourComponentVM extends ViewModelBase {}
2929

30-
const YourComponentView = observer(({ model }: ViewModelProps<YourComponentVM>) => {
31-
return (
32-
<div>
33-
{model.id}
34-
</div>
35-
)
36-
})
30+
export interface YourComponentProps extends ViewModelProps<YourComponentVM> {
31+
yourProp?: string;
32+
}
3733

38-
const YourComponent = withViewModel(YourComponent)(YourComponentView)
34+
const YourComponent = withViewModel(
35+
YourComponent,
36+
({ model, yourProp }: YourComponentProps) => {
37+
return <div>{model.id}</div>;
38+
},
39+
);
3940

4041
const YourApp = () => {
4142
return (
@@ -44,10 +45,8 @@ const YourApp = () => {
4445
}
4546
```
4647

47-
_wrapping into [`observer` HOC](https://mobx.js.org/api.html#observer) is optional if you are not using MobX observables_
48-
4948

50-
#### If you are using [`useCreateViewModel()` hook](/react/api/use-create-view-model)
49+
#### use [`useCreateViewModel()` hook](/react/api/use-create-view-model)
5150
Then you should render your React components with using this hook
5251

5352
```tsx
@@ -74,7 +73,7 @@ const YourApp = () => {
7473
```
7574

7675

77-
## 3. _[Optional]_ Using [ViewModelStore](/api/view-model-store/interface)
76+
## 3. _\[Optional\]_ Use [ViewModelStore](/api/view-model-store/interface)
7877
[ViewModelStore](/api/view-model-store/interface) is very powerful thing which allows you to lookup and get access to your view model instances everywhere.
7978
To use this store:
8079
1. Create instance of `ViewModelStore`

0 commit comments

Comments
 (0)