When I first started learning Jetpack Compose, one concept confused me a lot: State.
But once I understood it, everything became much easier.
Let me explain it in a simple way π
π What is State?
Think of State like a light switch.
When the switch changes β the light updates
When state changes β the UI updates
In Jetpack Compose, UI is automatically redrawn when state changes.
π§© Basic Example
@Composable fun CounterScreen () { var count by remember { mutableStateOf ( 0 ) }
Column { Text ( text = "Count: $count" )
Button ( onClick = { count ++ }) { Text ( "Increase" ) } } }
Enter fullscreen mode
Exit fullscreen mode
π§ Whatβs happening here?
remember β keeps value during recomposition
mutableStateOf β creates observable state
count++ β updates state β UI updates automatically
π You donβt manually refresh UI.β¦