Tlaster / PreCompose

Compose Multiplatform Navigation && State Management
https://tlaster.github.io/PreCompose/
MIT License
820 stars 49 forks source link

value didn't update in NavHost #271

Open RYXCP opened 6 months ago

RYXCP commented 6 months ago

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}")
        }
Tlaster commented 6 months ago

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

RYXCP commented 6 months ago

oh, thanks! It does work

martinbonnin commented 4 months ago

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

RYXCP commented 4 months ago

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.

martinbonnin commented 4 months ago

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

polis commented 3 months ago

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}") } } } } }

RYXCP commented 3 months ago

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.

polis commented 2 months ago

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.