Hi guys, I am reading this <viewmodel> document an...
# compose-android
k
Hi guys, I am reading this viewmodel document and it says
You should never pass down ViewModel instances to other composable
. Is there any strong recommendation for this? Can you please tell me which is better and why?
1. I like this way because of document
Copy code
val myModule = module {
    viewModel { MyViewModel() } 
}
------
Copy code
@Composable
fun MyComposable(viewModel: MyViewModel = koinViewModel()) {
}
2. My team saying that we need to use second approach something like this way. By setting the scope View Model like the sample below 👇
Copy code
val myModule = module {
    viewModel { MyViewModel(get()) } scoped(viewModelScope)
}
------
Copy code
@Composable
fun MyComposable() {
    val myViewModel: MyViewModel = viewModel()
}
c
this is fine, you should just not pass it on to composables called from your
MyComposable
for the same reason as https://kotlinlang.slack.com/archives/C04TPPEQKEJ/p1700385922837689
k
Thanks Christian, I'll take a look now.
a
I reason I can recall if you pass viewmodel/mutableState down the composable, then it can increase recomposition since compose will treat viewmodel or mutable state as unstable, so it will recompose for each call