remember e mutableStateOf
Armazene um estado que sobreviva à recomposição.
remember e mutableStateOf é uma aula grátis de Jetpack Compose Academy no CoddyKit. Esta é a aula 1 de 4. Você pode ler a aula completa abaixo gratuitamente — depois pratica ao vivo no navegador com um editor de código integrado e um tutor de IA 24/7. Faz parte do caminho de aprendizado de Jetpack Compose Academy, e seu progresso é sincronizado entre a web e o app CoddyKit. O curso de Jetpack Compose Academy inclui 4 aulas no total.
Partes desta aula ainda não foram traduzidas e aparecem em inglês.
What Is State?
In Compose, state is any value that can change over time and that your UI cares about, like a counter or a typed name. 🙂
Plain Variables Don't Work
A normal var inside a Composable changes its value, but Compose never notices, so the screen stays exactly the same.
var count = 0
Button(onClick = { count++ }) {
Text("Count: " + count)
}Meet mutableStateOf
Wrapping a value in mutableStateOf creates an observable holder. When its value changes, Compose knows it must redraw.
val count = mutableStateOf(0)
count.value = count.value + 1State Reads Are Tracked
Any Composable that reads a state's value gets subscribed to it automatically. Change the value and only those readers update.
The Recomposition Trap
Compose re-runs your function on every redraw. A fresh mutableStateOf each time would reset to zero and lose your work.
remember to the Rescue
remember stores a value across recompositions. Compose computes it once, then hands back the same instance on every redraw.
val count = remember { mutableStateOf(0) }The Classic Combo
You almost always pair them: remember keeps the holder alive, while mutableStateOf makes changes observable. Together they hold real UI state.
Cleaner with by
Use the by delegate so you read and write the value directly, skipping the noisy .value access every single time.
var count by remember { mutableStateOf(0) }
count++Putting It Together
Read count in your Text and change it in onClick. The button taps update the number, and Compose repaints just that Text.
var count by remember { mutableStateOf(0) }
Button(onClick = { count++ }) {
Text("Tapped " + count)
}State Holds Any Type
State is not just numbers. You can hold a String, a Boolean, or a whole data class, and Compose still tracks every change.
var name by remember { mutableStateOf("") }Keep State Local for Now
For small widgets, holding state right inside the Composable is fine. Later you will lift it up, but local state is the perfect starting point.
Quick Check
Why do we wrap mutableStateOf in remember?
Recap
You learned that remember plus mutableStateOf gives you state that survives recompositions and updates the UI automatically. Nice start! 🎉
Perguntas Frequentes
A aula “remember e mutableStateOf” é grátis?
Sim — o texto completo de “remember e mutableStateOf” é grátis para ler aqui na web. Para praticá-la interativamente (um editor de código integrado e um tutor de IA 24/7) e desbloquear o restante do curso de Jetpack Compose Academy, atualize para CoddyKit PRO. O curso de Jetpack Compose Academy inclui 4 aulas no total.
O que vou aprender em “remember e mutableStateOf”?
Armazene um estado que sobreviva à recomposição. Você pratica Jetpack Compose Academy com código prático que executa diretamente no navegador, e um tutor de IA 24/7 responde suas dúvidas enquanto trabalha na aula.
Preciso ter experiência prévia para começar Jetpack Compose Academy?
Nenhuma experiência prévia é necessária. Jetpack Compose Academy no CoddyKit é estruturado para alunos iniciantes até avançados, então você pode começar aqui ou desde o início e aprender no seu ritmo. Esta é a aula 1 de 4.
Quanto tempo leva a aula “remember e mutableStateOf”?
A maioria das aulas CoddyKit leva cerca de 5–10 minutos. Cada uma é compacta e interativa, então você faz progresso constante e retoma exatamente de onde parou entre web e app.
Posso escrever e executar código nesta aula de Jetpack Compose Academy?
Sim. Cada aula de Jetpack Compose Academy inclui um editor de código integrado, então você escreve e executa código real direto no navegador e recebe feedback de IA instantaneamente — nenhuma configuração local necessária.
Todas as aulas deste curso
- remember e mutableStateOf
- Recomposição: O que Aciona um Redesenho
- Criando um Contador de Toques
- rememberSaveable durante a Rotação