I'm working with Jetpack Compose for my app and have come across a scenario where I find it convenient to store lambdas as part of my UI state. Specifically, I have a data class that represents the state of a UI component, and in addition to traditional state data like Strings or Integers, I have lambdas for handling certain events.
data class MyTextFieldUiState(
val value: String = "",
val onFocusIsLost: () -> Unit = {},
val onValueChange: (String) -> Unit = {}
)
I utilize this state in a Composable as follows:
@Composable
fun MyTextField(uiState: MyTextFieldUiState) {
TextField(
value = uiState.value,
onValueChange = { newValue ->
uiState.onValueChange(newValue)
},
modifier = Modifier.onFocusChanged { focusState ->
if (focusState == FocusState.Inactive) {
uiState.onFocusIsLost()
}
}
)
}
My concerns are:
1. Does having lambdas as part of the state lead to any unexpected recompositions?
2. Is there a potential performance hit?
3. Is this pattern considered a best practice, or is there a more recommended way to handle event callbacks in Compose?
Any insights would be much appreciated!