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

Kevin Worth

03/16/2023, 6:04 PM
Would it be bad to call
viewModel()
at the top of a root composable, such as this:
Copy code
@Composable
fun PrimaryScreen() {
    val viewModel: PrimaryViewModel = viewModel()
    JokesTheme {
        // A surface container using the 'background' color from the theme
        Surface(
            modifier = Modifier.fillMaxSize(),
            color = MaterialTheme.colorScheme.background
        ) {
            val jokes by viewModel.jokes.collectAsState()
            JokeList(
                jokes = jokes
            )
        }
    }
}
Initially I put it as a parameter, as seen in this question over in #dagger and it appears I can get around that issue with the above code. So, does anyone see anything less than ideal with the code above? Given
viewModel()
is a
@Composable
, it seems like it should be fine, yeah?
b

bod

03/16/2023, 6:51 PM
2 Views