https://kotlinlang.org logo
#compose-android
Title
# compose-android
k

KotlinLeaner

11/20/2023, 9:18 AM
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

Chrimaeon

11/20/2023, 9:21 AM
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

KotlinLeaner

11/20/2023, 9:23 AM
Thanks Christian, I'll take a look now.
a

Ankit Kumar

11/21/2023, 6:10 PM
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
3 Views