useViewModel
Declares a ViewModel scoped to the store supplied by the tree's host. Root trees supply a LithoViewModelStore through ComponentTree.Builder.withLithoViewModelStore, while nested trees receive their current inherited store through TreeProps.
Without a key, one instance of T is retained for the owning store:
val model by useViewModel {
ProfileViewModel(repository)
}Use an explicit key when the same owner needs multiple instances of one ViewModel class:
val model by useViewModel(key = userId) {
ProfileViewModel(userId, repository)
}The same class and key resolve the same ViewModel across components that share the store, and a different key creates a different instance. Component identity and props are intentionally excluded. Values captured by factory are creation arguments rather than observable inputs; include changing identity in key or update the retained ViewModel explicitly. The factory may run on a Litho resolve thread and must not access Views or other main-thread-only objects. Reusable components that need independent instances of the same ViewModel class must receive a stable key from their owner.
Releasing the ComponentTree does not clear an owner-backed store. The Android owner clears its models when its lifecycle scope ends.