Eyjafl
Eyjafl

Reputation: 2185

MutableState callback in non-Composable

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

Upvotes: 1

Views: 637

Answers (1)

Thracian
Thracian

Reputation: 66674

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)
}

Upvotes: 2

Related Questions