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

Mehdi Haghgoo

02/27/2022, 3:59 AM
Is simply changing a local state variable in a composable considered a side effect?
p

Paul Woitaschek

02/27/2022, 9:36 AM
Yes
To answer wether this is bad depends on the context. For example in some cases it might be fine to let things hold their own state. For example the ripple animation
1
m

Mehdi Haghgoo

02/27/2022, 9:38 AM
But it's not mentioned under side effects docs
p

Paul Woitaschek

02/27/2022, 9:42 AM
You are right. I think the compose docs on side effects are mostly talking on the effect api
👍 1
But maybe I’m completely wrong and having a switch hold it’s own state is not a side effect at all but rather non state hoisting.
h

hfhbd

02/27/2022, 9:53 AM
Copy code
@Composable 
fun dummy() {
  var foo = 1
  TextField("Hello")
  foo = 2
  if (foo == 2) {
    TextField("2")
}
You should not expect seeing both TextField, and should use
mutableState
, which tells Compose to recompose (update) the execution. AFAIK ripple animation is a class containing mutableStates via a delegate, so updating these
local
variables are fine
1
2 Views