derivedStateOf を使っていない、よくないコード
- val state = rememberLazyListState()
-
-
- val showScrollToTopButton= state.firstVisibleItemIndex > 0
-
- LazyColumn(
- state = state,
- modifier = Modifier.fillMaxSize()
- ) {
- ...
- }
-
- if (showScrollToTopButton) {
- Button(
- onClick = {
- ...
- },
- ...
- ) {
- Text("scroll to top")
- }
- }
val state = rememberLazyListState()
// TODO derivedStateOf を使う
val showScrollToTopButton= state.firstVisibleItemIndex > 0
LazyColumn(
state = state,
modifier = Modifier.fillMaxSize()
) {
...
}
if (showScrollToTopButton) {
Button(
onClick = {
...
},
...
) {
Text("scroll to top")
}
}
LayoutInspector で Button が表示されたあともスクロールのたびに recompose が走っているので Button の skip count が増えていっています。
- val showScrollToTopButton by remember {
- derivedStateOf { state.firstVisibleItemIndex > 0 }
- }
val showScrollToTopButton by remember {
derivedStateOf { state.firstVisibleItemIndex > 0 }
}
に変えると、スクロールのたびに recompose が走っていたのがなくなり、Button の skip count が増えなくなりました。