Hey guys, Is there a way to improve this code: `...
# compose
r
Hey guys, Is there a way to improve this code:
Copy code
val items = remember {
    initialize items calulcation
}
SideEffect {
    update items calculation
}
to make it run only
initalize items caluclation
when composing for the first time?
e
That code already runs
initialize...
when composing for the first time only. Subsequent compositions will just return the remembered items (until the composable is removed from composition, in which case its remembered state is also removed from from composition/memory)
r
Yes, what I want is it to not run
update items calculation
when composing for the first time
e
Put it behind a boolean?
currentComposer.inserting
or something, although <-- that is not recommended. You can also maintain state in composition that is updated from the side effect
Copy code
val abc by remember { mSO(false) }

SideEffect { 
  if (!abc) abc = true else update items
}