Jetpack Compose: Not Able To Show Text In Textfield
Solution 1:
remember
is used just to ensure that upon recomposition, the value of the mutableStateOf
object does not get re-initialised to the initial value.
For example,
@ComposablefunTest1(){
var text by mutableStateOf ("Prev Text")
Text(text)
Button(onClick = { text = "Updated Text" }){
Text("Update The Text")
}
}
would not update the text on button click. This is because button click will change the mutableStateOf text
, which will trigger a recomposition. However, when the control reaches the first line of the Composable, it will re-initialise the variable text
to "Prev Text".
This is where remember
comes in.
If you change the initialisation above to
var text by remember { mutableStateOf ("Prev Text") }
,
It wil tell compose to track this variable, and "remember" its value, and use it again on recomposition, when the control reaches the initialisation logic again. Hence, remember over there acts as a "guard" that does not let the control reach into the initialisation logic, and returns that latest remembered value of the variable it currently has in store.
Post a Comment for "Jetpack Compose: Not Able To Show Text In Textfield"