casablancais
09/11/2023, 7:54 AMcasablancais
09/11/2023, 7:55 AMdata 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!xoangon
09/11/2023, 8:07 AMChristiano
09/11/2023, 8:09 AMBenoit Quenaudon
09/11/2023, 8:37 AMdata class MyTextFieldUiState(
val value: String = "",
)
and
the view would simply expose two events data object OnFocusIsLost
and data class OnValueChange(string)
which the presenter would react to.Benoit Quenaudon
09/11/2023, 8:41 AMBenoit Quenaudon
09/11/2023, 8:42 AMBenoit Quenaudon
09/11/2023, 8:46 AMcasablancais
09/11/2023, 8:55 AMAnd yo how would you test the lambda? You have to emit a model first, consume that model, and execute the lambda !?I will obtain an initial state, execute the lambda, and verify if the ViewModel/Presenter sends a new state with a new value. You are right; ensuring that the ViewModel/Presenter is in the correct state before executing the lambda can be a challenging task.
Stylianos Gakis
09/11/2023, 1:28 PMBenoit Quenaudon
09/11/2023, 1:35 PMStylianos Gakis
09/11/2023, 1:38 PMBenoit Quenaudon
09/11/2023, 1:39 PMStylianos Gakis
09/11/2023, 1:41 PM