https://kotlinlang.org logo
Title
e

elye

01/19/2022, 8:48 PM
Is the SideEffect
deriveStateOf
not able to work together with
snapshotFlow
in aiding recomposition? Separate them out seems working, but not together. The code example in https://stackoverflow.com/q/70771821/3286489
z

Zach Klippenstein (he/him) [MOD]

01/19/2022, 11:20 PM
Hm, that’s really weird. Will try to repro in a bit
e

elye

01/20/2022, 2:53 AM
Thanks. The more I try, the more I feel it’s a bug. But I cannot confirm so.
a

Alex Vanyo

01/20/2022, 2:58 AM
I can reproduce, and it is super weird… Here’s a more minimized version:
var count by remember { mutableStateOf(0) }
    var fromSnapshot by remember { mutableStateOf(false) }
    val fromDerived by remember { derivedStateOf { count > 0 } }

    LaunchedEffect(Unit) {
        snapshotFlow { count }.collect { fromSnapshot = it > 1 }
    }

    Column {
        Text("Button clicked at least 1 time: $fromDerived")
        Text("Button clicked at least 2 times: $fromSnapshot")
        Button(onClick = { count++ }) {
            Text("Increase count")
        }
    }
fromSnapshot
will be set to
true
when the button is clicked twice, but the text won’t be updated to reflect that.
a

Albert Chang

01/20/2022, 3:36 AM
What version of Compose are you using? If you are using 1.0, can you try 1.1 RC?
e

elye

01/20/2022, 3:48 AM
A bug report is file at https://issuetracker.google.com/issues/215402574. I’m using 1.0.5
👍 1
Thanks for all the inputs. I will try 1.1 and see too.
👀 1