rnett
08/19/2021, 2:29 AMrnett
08/19/2021, 2:51 AMclass SharedClicker{
private val actions = mutableMapOf<String, () -> Unit>()
fun setAction(key: String, action: () -> Unit){
actions[key] = action
}
fun removeAction(key: String){
actions.remove(key)
}
fun click(){
actions.values.forEach { it() }
}
}
@Composable
fun Repro(){
var pair by remember { mutableStateOf(0) }
val sharedClicker = remember { SharedClicker() }
Column(Modifier.padding(10.dp)) {
ReproPart("a", pair, sharedClicker){ pair = it }
Spacer(Modifier.height(20.dp))
ReproPart("b", pair, sharedClicker){ pair = it }
}
}
@Composable
fun ReproPart(key: String, n: Int, sharedClicker: SharedClicker, set: (Int) -> Unit){
val lambda = { set(n + 1) }
DisposableEffect(key, sharedClicker, lambda) {
sharedClicker.setAction(key, lambda)
onDispose {
sharedClicker.removeAction(key)
}
}
Row(Modifier.padding(4.dp).background(Color.Blue).clickable { sharedClicker.click() }){
Text(n.toString())
}
}
clicking one of the numbers should trigger both of the increment actions, and as such increment it twice. But it only does one, I suspect since the old value is captured in the action closure and not updated.darkmoon_uk
08/19/2021, 3:09 AMn
into ReproPart
, in its mutableState
form?darkmoon_uk
08/19/2021, 3:10 AMDisposableEffect
you're dealing directly with the State
.darkmoon_uk
08/19/2021, 3:10 AMrnett
08/19/2021, 3:21 AMSharedClicker
, not so much the particular lambdas.rnett
08/19/2021, 3:21 AM