myanmarking
01/06/2022, 12:35 PMvar isScrolling by remember {
mutableStateOf(lazyListState.isScrollInProgress)
}
LaunchedEffect(key1 = lazyListState){
snapshotFlow { lazyListState.isScrollInProgress }.collect {
isScrolling = it
}
}
Adam Powell
01/06/2022, 2:53 PMlazyListState.isScrollInProgress
directly?myanmarking
01/06/2022, 3:28 PMLaunchedEffect(item) {
snapshotFlow { pagerState.currentPage }.collect { page: Int ->
}
}
To be honest, i don’t quite know when to use remember vs a simple state variable. Can you point me to the right direction here? still learningAdam Powell
01/06/2022, 3:42 PMmyanmarking
01/06/2022, 3:46 PMstate.layoutInfo.visibleItemsInfo.firstOrNull { it.offset > 0 }?.index
If it produces the same output across recomposition, compose just skips it right? So in that case i don’t need remember right ?Adam Powell
01/06/2022, 3:46 PMmutableStateOf
when you need (a) observability (things recompose/relayout/redraw accordingly if it changes) and/or (b) transactionality (changes made in composition don't become "truth" until the composition or other snapshot successfully commits).Adam Powell
01/06/2022, 3:47 PMremember
when you need to persist the same value or object across multiple recompositions instead of creating new ones on every recompositionAdam Powell
01/06/2022, 3:48 PMderivedStateOf
Adam Powell
01/06/2022, 3:49 PMval firstIndex by derivedStateOf {
state.layoutInfo.visibleItemsInfo.firstOrNull { it.offset > 0 }?.index
}
myanmarking
01/06/2022, 3:49 PMmyanmarking
01/06/2022, 3:49 PMAdam Powell
01/06/2022, 3:49 PMderivedStateOf
returns a state object, so if you don't want to recreate that derived state object every recomposition, you can remember it and key it based on what should cause your code to create a new instance:
val firstIndex by remember(state) {
derivedStateOf {
state.layoutInfo.visibleItemsInfo.firstOrNull { it.offset > 0 }?.index
}
}
myanmarking
01/06/2022, 3:57 PMAdam Powell
01/06/2022, 4:03 PM