Home > Blockchain >  How to observe livedata in compose?
How to observe livedata in compose?

Time:10-21

I need to observe change in list which I am fetching from firebase

viewModel.courseList.observe(viewLifeCycleOwner,{

})

But I'm not able to use viewLifeCycleOwner.

CodePudding user response:

class MyViewModel: ViewModel() {
    private var _courseList = MutableLiveData<Whatever>()
    var courseList: LiveData<List<Whatever>> = _courseList
}

@Composable
fun MyComposable() {
    val list  = myViewModel.courseList.observeAsState().value
}

Demo app has plenty of examples: https://github.com/JohannBlake/Jetmagic

CodePudding user response:

use subscribe or ko.computed for detect any change in observable variable like

viewModel.courseList = ko.observable()
viewModel.courseList.subscribe((changedvalue)=>{
//to do code
});
  • Related