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

Dominaezzz

12/11/2020, 5:25 PM
Silly question but..... for
State
updates to work properly, do they have to be directly in a
@Composable
method? Would it still work with a non-
@Composable
wrapper method?
z

Zach Klippenstein (he/him) [MOD]

12/11/2020, 5:31 PM
You don’t need to use Composables at all with `State`/`MutableState`. It’s very common for regular classes to define
MutableState
properties.
You don’t even need composables to observe changes, see
snapshotFlow
However, if you’re using the snapshotting infrastructure without the rest of Compose, you’ll have to apply the global snapshot yourself somehow, or use more granular snapshots.
d

Dominaezzz

12/11/2020, 5:32 PM
That seems quite magical. My use case is to hide
State
and expose plain getters in a model.
z

Zach Klippenstein (he/him) [MOD]

12/11/2020, 5:32 PM
Using the low-level snapshot APIs is kind of tricky. Compose will apply the global snapshot once per frame for you automatically, which is where the “magic” comes from.
d

Dominaezzz

12/11/2020, 5:33 PM
Oh, it's global. I see.
z

Zach Klippenstein (he/him) [MOD]

12/11/2020, 5:33 PM
It’s really common to see stuff like:
Copy code
var yourState: String by mutableStateOf("hello")
  private set
d

Dominaezzz

12/11/2020, 5:33 PM
Thanks for explaining!
z

Zach Klippenstein (he/him) [MOD]

12/11/2020, 5:34 PM
Anyway, if you’re using this with other Compose UI, you don’t need to even know about the global snapshot or
snapshotFlow
.
3 Views