Side Effect: Produce State - devrath/ComposeAlchemy GitHub Wiki

About Produce State

  • Here say we want to create a compose state inside of UI by making use of suspend function by updating the state.
  • Usually it is not that useful since this state management is done using the view-model.
  • Basically we can provide a suspending function that returns a compose state inside the composable.
@Composable
fun ProduceStateDemo() {
    val counter by produceState(0) {
        // This provides a suspending function
        while (true){
            delay(1000L)
            value += 1
        }
    }

    Text(
        text = counter.toString(),
        modifier = Modifier.fillMaxSize().wrapContentSize()
    )
}