Can anyone tell me what scenarios `rememberUpdated...
# compose
c
Can anyone tell me what scenarios
rememberUpdatedState
applies to? I saw
rememberUpdatedState
is used in Compose Dialog to remember content, but I still can’t understand what describes in kdoc. Could someone please explain it to me. I am grateful.
👀 3
a
I think the doc is quite easy to understand. Basically it's used to create a value holder which always holds the latest value. If you still don't understand, reading the source of
rememberUpdatedState
may help.
Copy code
@Composable
fun <T> rememberUpdatedState(newValue: T): State<T> = remember {
    mutableStateOf(newValue)
}.apply { value = newValue }
As for the reason why it's used in
Dialog
, I believe it's that the
content
lambda is executed in another
setContent
call, which means that the content will not be correctly updated if recomposition is not explicitly triggered by the
State
.
❤️ 1
c
Thanks