Skip to content

Commit be77b8f

Browse files
committed
docs: better docs for useViewModel hook
1 parent 5f740de commit be77b8f

File tree

5 files changed

+101
-8
lines changed

5 files changed

+101
-8
lines changed

.changeset/puny-turkeys-itch.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"mobx-view-model": patch
3+
---
4+
5+
better documentation for `useViewModel` React hook

docs/.vitepress/config.mts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,19 @@ export default defineConfig({
184184
link: '/react/api/only-view-model',
185185
}
186186
]
187+
},
188+
{
189+
text: 'Errors',
190+
items: [
191+
{
192+
text: '#1: Active ViewModel not found',
193+
link: '/react/errors/1',
194+
},
195+
{
196+
text: '#2: ViewModel not found',
197+
link: '/react/errors/2',
198+
}
199+
]
187200
}
188201
],
189202
},

docs/react/errors/1.md

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# Minified error `#1`: Active `ViewModel` not found
2+
3+
This happened because [`vmLookup`](/api/other/view-model-lookup) for hook [`useViewModel`](/react/api/use-view-model) is not provided and hook trying to lookup active view model using `ActiveViewModelContext` which works only with using [`withViewModel`](/react/api/with-view-model) HOC.
4+
5+
6+
## Explanation:
7+
8+
This usage of hook [`useViewModel`](/react/api/use-view-model)
9+
10+
```tsx
11+
const model = useViewModel<YourVM>();
12+
```
13+
14+
Will use `ActiveViewModelContext` which exist only with usage [`withViewModel`](/react/api/with-view-model) HOC.
15+
16+
Also this can be happened if you are trying to get access to active `ViewModel` which is not exist in current React render tree:
17+
18+
```tsx
19+
<Component1>
20+
<ComponentAChild1>
21+
<Component>
22+
useViewModel<YourVM>()
23+
</Component>
24+
</ComponentAChild1>
25+
<YourVMComponent /> - this provide active `ViewModel` (withViewModel)
26+
</Component1>
27+
```
28+
29+
## Potential solution
30+
31+
The potential solution to this problem is to pass the [`vmLookup`](/api/other/view-model-lookup) to the hook [`withViewModel`](/react/api/with-view-model)
32+
33+
```tsx
34+
const model = useViewModel(YourVM);
35+
const model = useViewModel('idofyourvm');
36+
```
37+
38+
Or use `useViewModel<YourVM>()` with correct provided active `ViewModel` React render tree:
39+
40+
```tsx
41+
<YourVMComponent> - this provide active `ViewModel` (withViewModel)
42+
<Component1>
43+
<ComponentAChild1>
44+
<Component>
45+
useViewModel<YourVM>()
46+
</Component>
47+
</ComponentAChild1>
48+
</Component1>
49+
</YourVMComponent>
50+
```

docs/react/errors/2.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Minified error `#2`: `ViewModel` not found
2+
3+
This happened because your [`vmLookup`](/api/other/view-model-lookup) provided for hook [`useViewModel`](/react/api/use-view-model) is not found in [`ViewModelStore`](/api/view-model-store/overview).
4+
5+
6+
## Explanation:
7+
8+
Hook
9+
10+
```tsx
11+
const model = useViewModel(YourVM);
12+
```
13+
14+
Will use [`ViewModelStore`](/api/view-model-store/overview) to find your instance of provided `YourVM` [`ViewModel`](/api/view-models/overview).
15+
It means that your `ViewModel` is not created yet and not registered in `ViewModelStore` or you have not declared the creation of this `ViewModel` anywhere.
16+
17+
## Potential solution
18+
19+
Create and register your `ViewModel` using [`useCreateViewModel`](/react/api/use-create-view-model) hook or [`withViewModel`](/react/api/with-view-model) HOC.
20+
21+
```tsx
22+
const model = useCreateViewModel(YourVM);
23+
```
24+
25+
Also you can create and register your `ViewModel` using [ViewModelStore.attach() method](/api/view-model-store/interface#attach-viewmodel), but then you will need to completely reproduce the cycle of creating and destroying the `ViewModel`, which is described in the original code of the hook.

src/hooks/use-view-model.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,14 @@ export const useViewModel = <T extends AnyViewModel | AnyViewModelSimple>(
4343
if (process.env.NODE_ENV !== 'production') {
4444
console.error(
4545
'Active ViewModel not found.\n' +
46-
'This happens because "vmLookup" for hook "useViewModel" is not provided and hook trying to lookup active view model using ActiveViewModelContext which works only with using "withViewModel" HOC.\n' +
46+
'This happened because "vmLookup" for hook "useViewModel" is not provided and hook trying to lookup active view model using ActiveViewModelContext which works only with using "withViewModel" HOC.\n' +
4747
'Please provide "vmLookup" (first argument for "useViewModel" hook) or use "withViewModel" HOC.\n' +
48-
'See docs: https://js2me.github.io/mobx-view-model/react/api/use-view-model.html',
48+
'More info: https://js2me.github.io/mobx-view-model/react/errors/1',
4949
);
5050
}
51-
throw new Error('Active view model not found');
51+
throw new Error(
52+
'Minified error #1: https://js2me.github.io/mobx-view-model/react/errors/1',
53+
);
5254
}
5355

5456
if (process.env.NODE_ENV !== 'production') {
@@ -75,15 +77,13 @@ export const useViewModel = <T extends AnyViewModel | AnyViewModelSimple>(
7577
} else {
7678
throw new Error(
7779
`View model not found for ${displayName}.\n` +
78-
'This happens because your "vmLookup" provided for hook "useViewModel" is not found in "ViewModelStore".\n' +
79-
'See docs: https://js2me.github.io/mobx-view-model/react/api/use-view-model.html',
80+
'This happened because your "vmLookup" provided for hook "useViewModel" is not found in "ViewModelStore".\n' +
81+
'More info: https://js2me.github.io/mobx-view-model/react/errors/2',
8082
);
8183
}
8284
} else {
8385
throw new Error(
84-
`View model not found for ${displayName}.\n` +
85-
'This happens because your "vmLookup" provided for hook "useViewModel" is not found in "ViewModelStore".\n' +
86-
'See docs: https://js2me.github.io/mobx-view-model/react/api/use-view-model.html',
86+
'Minified error #2: https://js2me.github.io/mobx-view-model/react/errors/2',
8787
);
8888
}
8989
}

0 commit comments

Comments
 (0)