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

Tash

07/18/2022, 5:22 PM
Feel like I’m missing something super obvious; in `AbstractComposeView`s, should we be declaring click listeners and other “event” lambdas as `MutableState`s or just simple `var`s? I thought I noticed both approaches working 🤔. Code in 🧵
Copy code
class FooView : AbstractComposeView {
    // Consumers can set click listener
    // Does this need to be a mutable state?
    val clickListenerState: MutableState<() -> Unit> = mutableStateOf({})
    // Or does this suffice?
    var clickListenerNotState: () -> Unit = {}

    @Composable
    override fun Content() {
        Button(
            onClick = clickListenerNotState // OR clickListenerState.value
        )
    }
}
z

Zach Klippenstein (he/him) [MOD]

07/18/2022, 5:29 PM
If you change the value of that property after the initial composition, it needs to be in a snapshot state holder.
t

Tash

07/18/2022, 5:53 PM
Ahh 🤦🏼 I only tried with setting the listener as soon as
FooView
was inflated and didn’t try with subsequent changes to it (oh Mondays). Thanks for the sanity check 😅!
Thats interesting; I would assume that the content is already composed once the view is inflated, and therefore setting the value for the “first time” shouldn’t work either (but it does even if its not a snapshot state) 🤔
z

Zach Klippenstein (he/him) [MOD]

07/18/2022, 6:03 PM
The important thing is that it needs to be in a snapshot state if it changes, otherwise there's no guarantee the composition will see the latest values
👍🏼 1
5 Views