Skip to content

Instantly share code, notes, and snippets.

@alexaleluia12
Created October 16, 2023 16:14
Show Gist options
  • Save alexaleluia12/3035d05279f8ccc97be2110217c24d71 to your computer and use it in GitHub Desktop.
Save alexaleluia12/3035d05279f8ccc97be2110217c24d71 to your computer and use it in GitHub Desktop.
Composable Kotlin
// source - https://stefma.medium.com/jetpack-compose-remember-mutablestateof-derivedstateof-and-remembersaveable-explained-270dbaa61b8
@Composable
private fun CounterWithDerivedState() {
// salvar stado em caso a activity seja recaregada - rememberSaveable
val counterState = remember { mutableStateOf(0) }
val showHurrayState = remember {
derivedStateOf { counterState.value > 10 }
}
Button(
onClick = { counterState.value = counterState.value + 1 }
) {
Text(counterState.value.toString())
}
if (showHurrayState.value) Text("Hurray!")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment