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

Kyant

01/25/2023, 2:27 AM
How can I prevent
LaunchedEffect
running for the first time properly? The current solution is to add a "lock", but it is boilerplate.
j

jw

01/25/2023, 2:40 AM
Is it boilerplate or is it logic? It sounds like logic that you want to extract to a helper function.
k

Kyant

01/25/2023, 2:43 AM
I have tried to extract LaunchedEffect but it won't re-run unless I change the type of the param
keys
to
()->Any
s

Stylianos Gakis

01/25/2023, 1:08 PM
Are you reacting only to changes of a state object and not to the original value it is assigned? Could you try something like
Copy code
var myState by remember { mutableStateOf(1) }
LaunchedEffect(Unit) {
  snapshotFlow { myState }
    .drop(1)
    .collect { state ->
      //do something on each state change
    }
}
This should just drop the first state and run anything inside
collect
on every other change. Could it be something you can use?
k

Kyant

01/25/2023, 1:13 PM
Thank you @Stylianos Gakis , that's it.
169 Views