Psijic
Psijic

Reputation: 1002

Change state without recomposition

I need to change state in @Composable without its recomposition. How to do it properly?

val state by viewModel.state.collectAsState()

LaunchedEffect(currentValue) {
    if (currentValue == Done) viewModel.state.value = null
}

Update for comment: This is a subquestion for

Show BottomSheet only when the item is selected

Currently I need to resolve this intersection:

val selectedItem = viewModel.selectedItem.collectAsState()

LaunchedEffect(bottomSheetState.currentValue) {
    if (bottomSheetState.currentValue == SheetValue.Hidden) viewModel.selectItem(null)
}

if (selectedItem == null) LaunchedEffect(bottomSheetState.currentValue) { bottomSheetState.hide() }

Upvotes: 0

Views: 510

Answers (1)

Ashik Azeez
Ashik Azeez

Reputation: 444

var state by viewModel.state.collectAsState()  //change state to var and you can update 

  LaunchedEffect(currentValue) {
    if (currentValue == Done) state = null
}

Addition: If you want to check recomposition try

class Ref(var value: Int)
@Composable
fun LogCompositions(tag: String, msg: String) {
    if (BuildConfig.DEBUG) {
        val ref = remember { Ref(0) }
        SideEffect { ref.value++ }
        Log.d(tag, "Compositions: $msg ${ref.value}")
    }
}

Upvotes: 0

Related Questions