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

Rick Regan

08/11/2022, 5:20 PM
Is it OK to pass mutable state across a nested graph? I have screens in a
NavHost
where each
composable {}
contains a Composable function that is passed State representing the app’s settings (the State’s value actually, originating as
by mutableStateOf
above
NavHost
). I decided to separate out all the settings screens into a subgraph (same module) using an extension function called
NavGraphBuilder.settingsNavGraph
. In order to get the state to continue to track in the screens now moved into the subgraph, I pass a lambda to
settingsNavGraph
that reads the
.value
of the settings. (Locally, just above the
NavHost
call, I declared
val settingsAsState = rememberUpdatedState(settings)
in order to set this up.) This seems to work well, but I wanted to know if it was OK to do so, or if there is a better way. (The Compose Owl sample app does something similar, passing a
State<>
to the extension function – but it looks like it is the only sample app that does this.)
4 Views