https://kotlinlang.org logo
#compose
Title
# compose
s

Sean Proctor

12/07/2023, 11:43 AM
Can anyone suggest strategies for handling
TextField
data in a ViewModel that is shared between Compose and non-Compose targets?
When it was Compose-only, I was using
MutableState
. There's no validation done while changing values. I'm thinking to use a
MutableStateFlow
to get the initial values and only pass them back to the VM when submitting the form.
z

Zach Klippenstein (he/him) [MOD]

12/07/2023, 4:03 PM
You can use the MutableState from compose in non-compose-ui targets without depending on any of compose ui too, if you want.
s

Sean Proctor

12/07/2023, 4:09 PM
I don't think that I should bring the compose dependency in my JS target. Maybe I can expect/actual my way around this? I've read that collecting the state of
MutableStateFlow
like
.collectAsState(Dispacters.Main.immediate)
should avoid any async issues. Do you know if that's true?
z

Zach Klippenstein (he/him) [MOD]

12/07/2023, 4:17 PM
That doesn’t prevent you from introducing asynchrony into your flow in other ways. Eg some standard flow operators do this sneakily.
s

Sean Proctor

12/07/2023, 4:21 PM
Thanks. My usage is really simple. The values are loaded by the VM initially, then only modified by the TextField. Then they're read by the VM when the user hits a submit button.
👍🏻 1
3 Views