Home > Back-end >  How to recompose without onClick?
How to recompose without onClick?

Time:09-15

Why for recomposing, we must use an event like onClick from a Button?

for example, my code is:

if (movieSearchinfo.movieorserie == "serie") {

val serie1 = remember { mutableStateOf(false) }
if (serie1.value == true) {
    
                    Column(
                        modifier = Modifier
                            .verticalScroll(
                                rememberScrollState()
                            )
                            .height(100.dp)
                    ) {
                        LazyColumn(
                            modifier = Modifier
                                .padding(4.dp)
                        ) {
                            items(movieDetail.value) { movieDetail ->
                                if (movieDetail.se == "1") {
                                    serie1.value = true
                                    GetSerieCard(
                                        Modifier,
                                        movieDetail
                                    )
                                }
                            }
                        }
                    }

when after my last if, I say: serie1.value = true, why jetpack doesn't recompose and doesn't make the UI of my first if? because the serie1.value became true now?

I saw somewhere that we can use:

currentComposer.composition.recompose()

but I don't know what he meant. does anyone know how we can use it? I used it somehow but it didn't work. maybe I used it wrong

CodePudding user response:

Why for recomposing, we must use an event like onClick from a Button?

You don't necessarily need a button click for a Composable to recompose. Instance of MutableState that is read by a Composable, scope or conditional code block should change to trigger recomposition.

Value of MutableState can change in various ways including user interaction, finite or infinite animation, asynchronous operation result such as web request or query from a database returning a result.

For conditional code blocks like

if (serie1.value == true) {
   // Composable content 
}

condition must be true to enter recomposition. But in your snippet you change it to true if it enters Composition and then user clicks the Button which doesn't look possible with your snippet.

  • Related