I have two types of approaches, which is good? Cod...
# compose
k
I have two types of approaches, which is good? Code in thread 🧵
Copy code
@Composable
fun Body() {
	val lazyListState = rememberLazyListState()
	
	LazyColumn(state = lazyListState) {
		....
	}
	
	val jumpThreshold = with(LocalDensity.current) {
        56.dp.toPx()
    }
	
	val showScrollToTopButton = remember {
        derivedStateOf {
            lazyListState.firstVisibleItemIndex != 0 ||
                lazyListState.firstVisibleItemScrollOffset > jumpThreshold	
                            }
    }
	ScrollToTop(enabled = showScrollToTopButton.value)
}

@Composable
fun ScrollToTop(enabled: Boolean){....}
Copy code
@Composable
fun Body() {
	val lazyListState = rememberLazyListState()
	
	LazyColumn(state = lazyListState) {
		....
	}
	
	val jumpThreshold = with(LocalDensity.current) {
        56.dp.toPx()
    }
	
	val showScrollToTopButton = remember {
        derivedStateOf {
            lazyListState.firstVisibleItemIndex != 0 ||
                lazyListState.firstVisibleItemScrollOffset > jumpThreshold	
                            }
    }
	ScrollToTop(enabled = showScrollToTopButton)
}

@Composable
fun ScrollToTop(enabled: State<Boolean>){....}
In 1st one, whenever
showScrollToTopButton
changes it also recompose
Body
Composable. In 2nd one it don't. Both works, which way to follow, is to pass
State<Boolean>
or only
Boolean
b
The latter (passing Boolean) is the preferred, recommended approach
f
The derived state one