rememberUpdatedState
writes its state twice on initial composition. I wonder if it is possible to avoid this, and if so, under what circumstances it could make sense.
fun <T> rememberUpdatedState(newValue: T): State<T> = remember {
mutableStateOf(newValue)
}.apply { value = newValue }
For example, when there are several
rememberUpdateState
usages in one place, say 6 or 7 or 13, one might combine them into a single state holder object. Maybe in this case an optimization like that could make more sense?
class Holder(
prop1: Type1,
prop2: Type2,
//...
prop7: Type7
) {
var prop1 by mutableStateOf(prop1)
var prop2 by mutableStateOf(prop2)
//...
var prop7 by mutableStateOf(prop7)
}
val updatedState = remember { Holder(prop1, prop2, /*...*/, prop7) }.also {
it.prop1 = prop1
it.prop2 = prop2
//...
it.prop7 = prop7
}
I imagine if I used composer compiler plugin APIs directly then I could skip the extra assignments, for example like this:
if (!currentComposer.inserting) { it.prop1 = prop1 }