0

I am trying to update a compose UI view based on the state changes in the view model, below is my code Initially i am making assigning and doing operation by the default value from view model to a compose logic handling and after some time I will be making an API call which I am receiving the value and updating the state value in the VM fn but it is not reflecting the UI that I have assigned for operation, kindly let me know what I am missing

**View Model code**
var classAccessedByRefId:  MutableState<String> = mutableStateOf("")

// This is when I update the state once API is successful
    fun updateClassAccessId(classRefId: String) {
                classAccessedByRefId.value = classRefId
            }

**Compose code**

@Composable
fun BookingListItemView(classViewModel: ClassViewModel){
val classAccessedByRefId: String = bookingViewModel.classAccessedByRefId.value

if(classAccessedByRefId.value.isNotEmpty()){
                 // Do some operation if the classAccessedByRefId has value
                        }

}
arun
  • 95
  • 13
  • 1
    Why are you doing any of that `remember` / `mutableStateOf` when you already have state that is already stored separately from composition? Why not just access `bookingViewModel.classAccessedByRefId.value` directly? – ianhanniballake Jul 12 '23 at 03:51
  • I tried I didn't work out so I added a remember so if value gets hold to the variable and update on change @ianhanniballake, can you let me know how the scenario can be achieved via some code snippets – arun Jul 12 '23 at 03:53
  • @ianhanniballake i have edited the code by removing the "remember/mutablestateof" still no luck – arun Jul 12 '23 at 04:02
  • @arun try using `bookingViewModel.classAccessedByRefId.value.isNotEmpty()` directly in if statement – Jemshit Jul 12 '23 at 04:15

1 Answers1

0
@Composable
fun BookingListItemView(classViewModel: ClassViewModel){


LaunchedEffect(bookingViewModel.classAccessedByRefId.value){

   if(!bookingViewModel.classAccessedByRefId.value.equals("")){
                 // Do some operation if the classAccessedByRefId has value
                        }
}


}
zaid khan
  • 825
  • 3
  • 11