Hi guys , I was exploring MVVM with jetpack compos...
# android
d
Hi guys , I was exploring MVVM with jetpack compose. But want to understand how to deal with side effects in compose?
j
d
Bro I know side effects in compose. Just wanted to know how to emit conditional side effects from view model to view such as showing snackbar doing navigation etc.
j
well you could use something like a sharedflow to handle one offs for different EventStates , ex navigation, snackbar
c
So if you use a view model, there is not need to use compose side effects. Just emit the new “state” from your viewmodel.
Have a look at the “now in android” github repo, it’s a very good source for such questions.
d
But showing a snackbar going to a next screen is this really required to be present in the ui state?
c
A snack bar is usually tied to a scaffold - so actually the snack bar is a UI state for the scaffold.
b
Of course your composables will observe some state object provided by the viewmodel. To show a snackbar, for instance, you'll use a LaunchedEffect with the view state field as the key such that it will update whenever that field is updated.
Alternatively, your viewmodel can own and expose the actual state object that your composables observe.