Open ellykits opened 5 months ago
Can you provide more example of this bug? since I'm not familiar with voyager and cannot reproduce this issue.
Hey! Not connected to op, but i have the same issue and I've assembled a simple example for this.
Repro steps:
Expected behaviour: Every time the dialog is opened, the koinViewModel
function should return a new view model instance.
Let me know if I can help any other way.
The reason koinViewModel
returns the same instance is the same as #294, koinViewModel
is seeking for viewModel from a StateHolder
, which in this case is the root StatsHolder
from PreComposeApp
, so it does return the same viewModel every time, this is the current behaviour.
Ah I see. In android it seems like a composable function in itself is a state holder but of course I'm not sure how it works under the hood. Here we just need to work around this by clearing/resetting the view model when the composable dismisses. Something like this will make this easier so i don't have to rewrite it every time for each composable that uses a VM.
@Composable
inline fun <reified VM : ViewModel> ComposableWithVM(vm: VM = koinViewModel<VM>(), content: @Composable (VM) -> Unit) {
DisposableEffect(Unit) {
onDispose {
vm.clear()
}
}
content(vm)
}
Thank you for the answer!
Describe the bug
I'm using Precompose in an Android application, I however noticed each time I navigate to the same Scene that uses the same
ViewModel
, the sameViewModel
instance is returned.To Reproduce For context see a similar issue reported on
voyager
https://github.com/adrielcafe/voyager/issues/7Expected behavior A new
ViewModel
should be constructed with each transition to a scene.Minimal reproducible example Very similar to https://github.com/adrielcafe/voyager/issues/7