https://kotlinlang.org logo
#compose
Title
# compose
o

Oscar Sequeiros

03/22/2020, 9:14 PM
Is there a way to write @Composable functions after async calls?
l

Leland Richardson [G]

03/22/2020, 11:23 PM
it’s not clear to me what you’re asking. what is it you’re trying to do?
o

Oscar Sequeiros

03/22/2020, 11:55 PM
Hi Leland. I have the code bellow:
Copy code
private fun bind() {
        disposables.add(viewModel.states().subscribe(this::render))
        viewModel.processIntents(intents())
    }

    private fun render(state: ItemsViewState) {
    }
I want to know how I could call a composable function into render, I know that the composable functions can only be called from another composable or setContent function.
l

Leland Richardson [G]

03/23/2020, 1:49 AM
what you want to do instead is set up your composable functions to render things based on some stateful piece of data using the
state { … }
composable. Then when you subscribe to
vviewModel.states()
you can update that value which will cause a recomposition. for instance, you could do something like this:
Copy code
@Composable fun ComposeState(state: ItemsViewState) { ... }
Copy code
setContent {
  val x = state<ItemsViewState?> { null }
  onActive {  viewModel.states().subscribe { x.value = it } }
  x.value?.let { ComposeState(it) }
}
Something like that. There are lots of different ways to do this, some better than others depending on the context, but hopefully this can get you started?
o

Oscar Sequeiros

03/23/2020, 3:21 AM
I see. I appreciate your help! I'm going to read about state and onActive.
3 Views