Hello everyone, I know that composable functions m...
# compose
f
Hello everyone, I know that composable functions must be side-effects free. If I update the value of a global variable in onClick callback the function is side-effect free??? @Composable fun Test() { Button( onClick = { globalVariable++ } ) }
s
What do you mean by global, where is it defined?
a
The code inside onClick lambda is not executed inside composition, so yes, this Composable is perfectly valid from a Compose point of view.
👆 1
Having mutable global variables is another conversation, unrelated to Compose 🙂
1
f
That global variable is inside of a singleton repository
It is not shared preference and does not have expansive computation so I think valid the possibility of updating it from onclick lambda
c
AFAIK, composables should typically be side effect free, but sometimes you do need sideeffects. like LaunchedEffect.
f
yeahh colton
but in my example I can update the variable directly from onclick lambda or I need of LaunchedEffect??
s
You can do that, but where is that variable defined, and how is it defined? That's pretty important here.
f
Tha variable is just a boolean defined inside of the a singleton repository
s
Welp, sure you can do that, but that feels fishy af. How are you using that boolean? Are you trying to observe it then from some composable?
f
that boolean is used just to configure the repository, depends on its value the repository fetch data of different local. The screen that have the composable above only to update the variable, but I have others screen that observe and update it, but in this case more complex I use MVI pattern, currently I am using MVIKotlin -> https://github.com/arkivanov/MVIKotlin
s
Okay, at least have your composable take a lambda which does that instead of directly accessing something from inside your Repository which would make it impossible to preview your composable.
👍 3
a
Well, I believe technically it's side-effect free. I think a side-effect would be changing the state directly in the Composable function (the callback is not Composable). So you are fine. However, accessing global things doesn't look good from the architecture perspective.
f
Nice, I changed the access, now is via MVI Store
K 1