Skip to content

Error #2: ViewModel not found

This happened because the vmLookup provided for the useViewModel hook was not found in ViewModelStore.

Explanation:

Hook

tsx
const model = useViewModel(YourVM);

Will use ViewModelStore to find your instance of provided YourVM ViewModel.
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.

HMR (Hot Module Replacement)

This error can also happen during development with HMR. When you edit a ViewModel class (add/remove a property, change a method, etc.), HMR replaces it with a new class — but the instance in ViewModelStore still belongs to the old one. The lookup can't find it and throws.

Potential solution

Create and register your ViewModel using useCreateViewModel hook or withViewModel HOC.

tsx
const model = useCreateViewModel(YourVM);

You can also create and register your ViewModel using the ViewModelStore.attach() method, but then you will need to reproduce the full creation and destruction cycle, which is implemented inside the hook.

Fixing HMR issues

If the error only appears after editing a ViewModel class during development, use the mobx-view-model-vite-plugin — it automatically reconciles the old and new constructors on HMR updates so the lookup continues to work without a full page reload.

Released under the MIT License.