What is the best approach, if you want to check va...
# compose
p
What is the best approach, if you want to check value from the previous recomposition? For example:
Copy code
fun Pin(pin: String) {
    val previousPin: String = TODO()
    val isDeleting = pin.length < previousPin.length
}
j
That's what remember is for! Just make sure you don't use a key to ensure it's only remembered positionally in the composition. You probably want to use a
SideEffect
to write the new value to ensure only completed recompositions update it.
👍 1
p
That was my initial idea yeah, thanks. I'm wondering if there is something better around.
Copy code
val lastPin = remember { mutableStateOf(pin) }

val isDeleting = lastPin.value.length > pin.length

LaunchedEffect(pin) {
    lastPin.value = pin
}
s
You don't want to use mutableStateOf here, it triggers an additional recomposition. Consider using some wrapper with a singular
var
field
Copy code
class Ref<T>(var value: T)

val lastPin = remember { Ref(pin) }
SideEffect {
  lastPin.value = pin
}
😲 1
👍 1