My team is trying to find a way to communicate bet...
# compose-desktop
s
My team is trying to find a way to communicate between a parent composable and a child composable while using the Voyager navigation library. Does anyone have any thoughts?
s
So, don’t know exacly your use case, but I have todo some workarounds in order to proper use Voyager BottomSheetNavigator + Navigator. We had to be to open a bottomsheet and a button click on it could navigate to other screen. And this was not working because the Composable/Context of the BottomSheet, when calling
LocalNavigator.current
will never return the Navigator for the
BottomSheetNavigator { Navigator(MyScreen) }
Don’t know if you are trying to do something like that, maybe you can explain a little bit.
Copy code
class NavigatorReference(
    val navigator: Navigator
)

val LocalNavigatorReference: ProvidableCompositionLocal<NavigatorReference> =
    staticCompositionLocalOf { error("NavigatorReference not initialized") }

@Composable
private fun ProvideNavigatorReference(
    navigator: Navigator,
    content: @Composable () -> Unit
) {
    CompositionLocalProvider(LocalNavigatorReference provides NavigatorReference(navigator)) {
        content()
    }
}

@Composable
fun BottomSheetWithNavigatorReference() {
    var currentNavigator by remember { mutableStateOf<Navigator?>(null) }
    
    BottomSheetNavigator(
        sheetContent = {
            // Current Navigator will never be null because `SheetContent` will always be called
            // when it already called `content`.
            ProvideNavigatorReference(currentNavigator!!) {
                CurrentScreen()
            }
        },
        content = { bottomSheetNavigator ->
            Navigator(screen = YourStartScreen()) { navigator ->  
                LaunchedEffect(navigator) {
                    currentNavigator = navigator
                }
                ProvideNavigatorReference(navigator) {
                    CurrentScreen()
                }
            }
        }
    )
}
With this approach you can navigate from BottomSheet
But you have to navigate using
LocalNavigatorReference.current.navigator
(you can do a better API for it using Composable property to look like
LocalNavigator.current
)