Billy Newman
11/03/2023, 9:02 PMBilly Newman
11/03/2023, 9:14 PMModalBottomSheetLayout(bottomSheetNavigator) {
NavHost(
navController = navController,
startDestination = "main"
) {
homeGraph(
// Nothing in this lamda and still seeing re-compose
showSnackbar = { /* no-op */ }
)
}
}
fun NavGraphBuilder.homeGraph(
showSnackbar: (String) -> Unit
) {
childGraph(
share = { showSnackbar(it) },
)
}
fun NavGraphBuilder.childGraph(
onShare: (String) -> Unit,
) {
composable("childDetail) {
// Recompose happens here. However I am seeing a re-componse in the DetailScreen
// whenever there is a re-compose at this level.
DetailScreen(
onShare = { onShare(it) }
)
}
}
As far as I understand if a lambda does not wrap anything that is not stable the lambda itself is remembered and not subject to re-compose. However in this case when the childDetail composable is re-composed the DetailScreen is seeing the onShare lambda change.
This is fixed if I forcefully remember the lambda:
fun NavGraphBuilder.childGraph(
onShare: (String) -> Unit,
) {
composable("childDetail) {
val currentOnShare = remember(Unit) { onShare }
// Recompose happens here. However I am seeing a re-componse in the DetailScreen
// whenever there is a re-compose at this level.
DetailScreen(
onShare = { currentOnShare(it) }
)
}
}
However I don’t think that is necessary. What am I missing?xoangon
11/04/2023, 7:47 AMBilly Newman
11/04/2023, 2:40 PM