What is a State - devrath/ComposeAlchemy GitHub Wiki

1_jO0F7-j-sbk3PxXokxW21w

State

  • Almost all the applications update the UI dynamically, In the jetpack compose, We manage it by modifying the state.
  • State can exist over time and change course over time.
var text by remember{ mutableStateOf("") }
  • What Remember does is, It makes the variable exist over time in recompositions.
  • What mutableStateOf does is, It tells that it can change and it is like the mutableLiveData but we explicitly do not need to observe it.
  • what by does is, It is a delegation property.

State vs Variables

  • In declarative languages(ex: Compose), State is referred to as something that can change over time.
  • The State differs from a normal variable in 2 different ways.
    • A state in a composable function needs to be remembered so that it can be used across the compositions.
    • Another thing is state in the UI has consequences in the entire view hierarchy

State,MutableState,MutableStateOf

State

  • Its read-only value T.
  • It notifies composition and observers when the value changes.

MutableState

  • It is an extension of the state.
  • Here we can update the value of the state to a new value.
  • It notifies composition and observers when the value changes.

MutableStateOf

  • It is same as mutableState but it has a initial value.