Home > Blockchain >  MutableState callback in non-Composable
MutableState callback in non-Composable

Time:08-14

I have a MutableState -- how to create a callback that will be called each time its value changes in a non-Composable function?

CodePudding user response:

I think SnapshotFlow is what you might need. It also lets you filter, map use any Flow operator that you might need too.

val stateQuery by remember { mutableStateOf("") }

LaunchedEffect(Unit) {
    snapshotFlow {
        stateQuery
    }
        .distinctUntilChanged()
        .onEach { query ->
            viewModel.search(query)
        }
        .launchIn(this)
}
  • Related