Home > Back-end >  Is it bad to inject two ViewModel objects into a composable function?
Is it bad to inject two ViewModel objects into a composable function?

Time:09-02

I'm using Hilt for injecting regular objects, as well as ViewModels. In a single part of my app, there is a composable function where I need to inject two ViewModel objects:

@Composable
fun AllProducts(
    allProductsViewModel: AllProductsViewModel = hiltViewModel(),
    authViewModel: AuthViewModel = hiltViewModel()
) {
    //Stuff
}

Is it bad to inject two ViewModel objects rather than a single, as I do in the other parts of my application? Are there any downsides?

CodePudding user response:

This pulls the idea of having two models describing your ui state.

The only downside is how you later provide your @PreviewParameter in your composable previews.

Under assumption that there will be two models that describe your composable state, you will need to tinker with the previews, where you will be forced to introduce third class that will just be used for the preview parameters and nowhere else.

  • Related