I have a view model which exposes a flow of events. I have that viewModel injected in a top level composable but I want to send the events to another composable (inside the top level composable). Something like this:
@Composable
fun MainScreen(viewModel: MyViewModel) {
SomeComponent(viewModel.events)
// Other Stuff
}
@Composable
fun SomeComponent(events: Flow<MyEvent>) { // MyEvent is a sealed class
LaunchedEffect(Unit) {
events.collect {
// Process the events
}
}
// Other stuff
}
The reason I am collecting the flow in SomeComponent and not MainScreen is because I need access to some internal state of SomeComponent (it's a ScaffoldState actually) for processing the events.
I wrote this code in my app and everything seems to be working but I am not sure if this is a good thing to do.
My question is:
Are there any drawbacks in this approach that I missed? What can possibly go wrong here?