What's the difference between this two approaches? And why first is preferable?
Copy code
@Composable
fun TestScreen(viewModel: TestViewModel = hiltViewModel()) {
//content
}
Copy code
@Composable
fun TestScreen() {
val viewModel: TestViewModel = hiltViewModel()
//content
}
l
Luis
09/08/2021, 4:55 AM
Using hilt, you can easily mock the view model in both cases. But the first one allows you to pass in a ViewModel without having to setup hilt at all, so you would usually go with that one
c
Colton Idle
09/08/2021, 4:47 PM
The top option has it's viewModel "dependency" injected (passed through constructor) so it's typically easier to swap it out for testing.