When is a lambda that is captured in a lamda not s...
# compose
b
When is a lambda that is captured in a lamda not stable? I trying to track down issues with a recompose that I don’t think should be happening. Example in thread
Copy code
ModalBottomSheetLayout(bottomSheetNavigator) {
      NavHost(
         navController = navController,
         startDestination = "main"
      ) {
         homeGraph(
            // Nothing in this lamda and still seeing re-compose
            showSnackbar = { /* no-op */ }
         )
      }
}
Copy code
fun NavGraphBuilder.homeGraph(
   showSnackbar: (String) -> Unit
) { 
   childGraph(
      share = { showSnackbar(it) },
   )
}
Copy code
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:
Copy code
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?
x
You can use the Compose Compiler metrics to check the stability of your @Composable function arguments. There were people reporting unstable lambdas and in this article you can understand the why
b
I do understand the concept of using unstable values in a lambda and recomposition. However I don’t understand what is unstable about any of the lambdas above