Mihai Voicescu
11/11/2021, 2:47 PMvar chats by useState(listOf<String>("first"))
useEffectOnce {
GlobalScope.launch {
client.rtmStream().collect {
val current = chats // always initial value
println(current)
chats = current + it.message
}
}
}
Am I doing something wrong?Daniel Rampelt
11/11/2021, 3:05 PMuseState
returns an array containing the value and a function to set the value, so the value is captured in that array and isn't actually a function to get the current value. The getter function allowing you to use by
just accesses the first element in that array which is just the captured value:
inline operator fun getValue(
thisRef: Nothing?,
property: KProperty<*>,
): T =
asDynamic()[0]
Mihai Voicescu
11/11/2021, 3:07 PMDaniel Rampelt
11/11/2021, 3:07 PMuseRef
instead
https://stackoverflow.com/a/60643670Daniel Rampelt
11/11/2021, 3:13 PMvar chats by useState(listOf<String>("first"))
val chatsRef = useRef(chats)
chatsRef.current = chats
Then in your callback you can access chatsRef.current
to get the current valueturansky
11/11/2021, 3:22 PMcleanup
missedturansky
11/11/2021, 3:23 PMMihai Voicescu
11/11/2021, 3:35 PMMihai Voicescu
11/11/2021, 3:39 PMturansky
11/11/2021, 4:47 PMCLOVIS
11/21/2021, 6:38 PMcleanup
part to its own hook: https://gitlab.com/clovis-ai/formulaide/-/blob/main/ui/src/main/kotlin/formulaide.ui/components/Async.kt#L20CLOVIS
11/21/2021, 6:39 PM