Hey folks, is there a concept of viewModel in compose desktop?
e
Hey folks, is there a concept of viewModel in compose desktop?
b
Concept - yes, it's just a concept afterall so it's framework ambiguous. If you're looking for dedicated API, then no. But you can achieve very similar result with regular view model classes that contain mutable state fields
🤙🏾 1
e
That's the direction I was headed but didn't want to reinvent. Thanks!
b
e.g.:
Copy code
class MyViewModel {
  var count by mutableStateOf(0)
}

@Composable
fun Component() {
  val vm = remember{MyViewModel()}
  Button(
    text = vm.count.toString(),
    onClick = {vm.count++}
  )
}
e
That looks about right. This follows jetpack compose state holders as the source of truth option.
m
Some multiplatform navigation libraries come with a implementation of ViewModels. Voyager for instance comes with ScreenModel that works really well with its navigation
👆🏻 1
👆 1
👍🏾 1
m
I prefer to use StateFlows in my view models to keep them independent from any compose libraries.
🤙🏾 1
s
Take a look at Voyager one: https://voyager.adriel.cafe/screenmodel
t
2848 Views