would it be a bad idea, to have a CompositionLocalProvider for a callback, to communicate with root composable, like post msg on a msg bus?
Peter Mandeljc
09/15/2021, 11:07 AM
Copy code
typealias Callback = (String) -> Unit
// Fixme: composition policy
val LocalActiveCallback = compositionLocalOf<Callback> { error("No active callback") }
@Composable
@Preview
fun Parent() {
val callback: Callback = { msg ->
Timber.e(msg)
}
CompositionLocalProvider(LocalActiveCallback provides callback) {
Child()
}
}
@Composable
private fun Child() {
val callback = LocalActiveCallback.current
Button(onClick = {
callback("hello")
}) {
Text("call me")
}
}
c
Csaba Kozák
09/15/2021, 11:11 AM
I also used this technique to communicate from a tab screen to the root screen, as i needed to animate the bottom nav according to some state in a very deep composable function. I would try to avoid this as possible, still it is working in my case.
k
K Merle
09/15/2021, 6:51 PM
It looks super handy indeed. My main concern is testing, after that, hiding implementations. It could get very hard to debug I would imagine if app gets big.