Skip to content
🤔 Documentation issue? Report or edit

State

Define a state

val textState = mutableStateOf("Hello")

You can use the mutableStateOf function to create a mutable state.

Example

In this example we will create a composable with a Text and a Button. On a click on the button, the count state will go up and the text of Text will be updated.

Initial state:

After Button click:

@Composable
fun StateDemo(){
    val countState = remember { mutableStateOf(0) }
    Column {
        Button(colors = ButtonDefaults.buttonColors(backgroundColor = MaterialTheme.colors.secondary), onClick = { countState.value++ }) {
            Text("count up")
        }
        Text("You have clicked the Button " + countState.value.toString() + " times")
    }
}
val countState = remember { mutableStateOf(0) }

Here we define the state for the click counter with mutableStateOf(0). 0 will be the initial value. Because 0 is an Int, the counterState will only allow values which are Int.

remember is used to remember the countstate. Without remember, every time the value of countstate would change, the StateDemo Composable will be recomposed and your state will also get recreated with the initial value. When you use remember it will remember the last value and not be recreated.

countState.value
With the value property you can get/set the value of the counterstate. In the onClick() function of the Button, the value will be incremented. The Text shows the value of the counterstate. When counterstate is changed, “Text” will also change.

See also:


Last update: September 3, 2021
Back to top