how do I call suspend JS functions like runBlockin...
# multiplatform
i
how do I call suspend JS functions like runBlocking in Android and Native?
j
JS can't block its thread like Android and native. You can
launch
a coroutine and run suspend functions within the coroutine. Or make the
main()
function itself a
suspend fun
.
i
here in code in questioned
Copy code
@Composable
fun FilterTabs(tabState: MutableState<TabState>, scrollState: ScrollState) {
    TabRow(selectedTabIndex = TabState.values().toList().indexOf(tabState.value)) {
        TabState.values().forEach {
            Tab(
                text = { Text(it.name) },
                selected = tabState.value == it,
                onClick = {
                    tabState.value = it
                    runBlocking {
                        scrollState.scrollTo(0)
                    }
                }
            )
        }
    }
}
j
You should do this instead:
Copy code
@Composable
fun FilterTabs(tabState: MutableState<TabState>, scrollState: ScrollState) {

    val coroutineScope = rememberCoroutineScope()

    TabRow(selectedTabIndex = TabState.values().toList().indexOf(tabState.value)) {
        TabState.values().forEach {
            Tab(
                text = { Text(it.name) },
                selected = tabState.value == it,
                onClick = {
                    tabState.value = it
                    coroutineScope.launch {
                        scrollState.scrollTo(0)
                    }
                }
            )
        }
    }
}
coroutineScope
is tied to the composable's lifecycle. This way your launched coroutine, and suspend functions called within in it, will be canceled when the composable's lifecycle ends.
i
thanks