Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compose is skipping state #34

Closed
katajona opened this issue Apr 3, 2023 · 1 comment
Closed

Compose is skipping state #34

katajona opened this issue Apr 3, 2023 · 1 comment

Comments

@katajona
Copy link

katajona commented Apr 3, 2023

When I trigger two dispatch functions one after anther the first state gets skipped.
Here is an example:

data class State(val name: String = "test")
sealed interface Action {
    data class Rename(val name: String) : Action
}

val reducer: Reducer<State> = reducerForActionType<State, Action> { state, action ->
    when (action) {
        is Action.Rename -> state.copy(name = action.name)
    }
}

@Composable
fun App() {
    StoreProvider(createStore(reducer, State())) {
        Component()
    }
}

@Composable
fun Component() {
    val name by selectState<State, String> { this.name }
    val dispatch = rememberDispatcher()
    LaunchedEffect(name){
        println(name)
    }
    LaunchedEffect(Unit){
        dispatch(Action.Rename("a"))
        dispatch(Action.Rename("b"))
    }
    Text(text = name)
}

When running the following code I can see on the console the following:

I/System.out: test
I/System.out: b

What I would expect:

I/System.out: test
I/System.out: a
I/System.out: b

When further having a look into it and adding a delay(1) between the two dispatch events, I can get inconsistent outputs:
Output 1:

I/System.out: test
I/System.out: b
I/System.out: b

Output 2:

I/System.out: test
I/System.out: a
I/System.out: b

Output 3:

I/System.out: test
I/System.out: b
@mpetuska
Copy link
Contributor

This is expected behaviour for compose as multiple updates suring recomposition cycle are squashed to the latest before rendering.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants