Tlaster / PreCompose

Compose Multiplatform Navigation && State Management

Home Page:https://tlaster.github.io/PreCompose/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

value didn't update in NavHost

RYXCP opened this issue · comments

In 1.5.10, based on the sample app called Reply, I gave it a try. However, I found that the screenType in the scene retains its initial value while the one outside the NavHost is changed correctly.

Should I not have done this?

//This is Reply
val contentType: ReplyContentType = ReplyContentType.SINGLE_PANE
...
@Composable
private fun ReplyNavHost(
    navController: NavHostController,
    contentType: ReplyContentType,
    ...
) {
    NavHost(
        navController = navController,
        startDestination = ReplyRoute.INBOX,
    ) {
        composable(ReplyRoute.INBOX) {
            ReplyInboxScreen(
                contentType = contentType,
                ...
//This is my app
var screenType = ScreenType.MEDIUM
...
@Composable
fun NavGraph(
    navController: Navigator,
    screenType: ScreenType,
){
    Text("${screenType}")
    NavHost(
        navigator = navController,
        initialRoute = Route.CHAT_ROUTE,
    ){
        scene(
            route = Route.SETTING_ROUTE
        ){
            Text("${screenType}")
        }

Can you try 1.6.0-beta02 and check if it's working for you?

oh, thanks! It does work

I'm having a similar issue on 1.6.0. Any hint how to debug this?

I'm having a similar issue on 1.6.0. Any hint how to debug this?

When i tried the "1.6.0-beta02", i found that it resolved this issue on its own.

Thinking more about it, I think this might be the desired behaviour or else deeplinks might break. All the parameteres should come from the deeplink url. But not 100% sure yet

I have a very similar issue in 1.6.0-rc05.
The only difference in my case that I use 2 NavHost components and one is nested into other. And Screen in nested NavHost retains its initial value.

Here is example:

var screenType = ScreenType.MEDIUM
...
@composable
fun NavGraph(
navController: Navigator,
screenType: ScreenType,
) {
Text("${screenType}")
NavHost(
navigator = rememberNavigator(),
initialRoute = Route.CHAT_ROUTE,
) {
scene(
route = Route.SETTING_ROUTE
) {
NavHost(
navigator = rememberNavigator(),
initialRoute = Route.ENOTHER_ROUTE,
) {
scene(
route = Route.ENOTHER_ROUTE
) {
Text("${screenType}")
}
}
}
}
}

If you find that the screen always displays the previous value which is outdated, maybe these will help:

In current version, updating the value outside of a NavHost could not trigger the recomposition( I guess) , namely passing values directly into a NavHost is not recommended.

It seems like you want to observe to the window size, perhaps setting up a kotlin object and check it in somewhere or simply delaying the update to NavHost scope, will be better.

If you find that the screen always displays the previous value which is outdated, maybe these will help:

In current version, updating the value outside of a NavHost could not trigger the recomposition( I guess) , namely passing values directly into a NavHost is not recommended.

It seems like you want to observe to the window size, perhaps setting up a kotlin object and check it in somewhere or simply delaying the update to NavHost scope, will be better.

Thank you for suggestion! I moved to Navigation from JetBrains. It still in Alfa but it does not have this problem and my project is not commercial yet.