remember e mutableStateOf
Memorizzi uno stato che sopravvive alla ricomposizione.
remember e mutableStateOf è una lezione Jetpack Compose Academy gratuita su CoddyKit. Questa è la lezione 1 di 4. Puoi leggere la lezione completa qui gratuitamente — poi esercitati direttamente nel browser con un editor di codice integrato e un tutor IA disponibile 24/7. Fa parte del percorso di apprendimento Jetpack Compose Academy, e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso Jetpack Compose Academy include 4 lezioni in totale.
Parti di questa lezione non sono ancora state tradotte e vengono mostrate in inglese.
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! 🎉
Domande Frequenti
La lezione «remember e mutableStateOf» è gratuita?
Sì — il testo completo di «remember e mutableStateOf» è gratuito qui sul web. Per esercitarvi in modo interattivo (un editor di codice integrato e un tutor IA 24/7) e sbloccare il resto del corso Jetpack Compose Academy, passa a CoddyKit PRO. Il corso Jetpack Compose Academy include 4 lezioni in totale.
Cosa imparerò in «remember e mutableStateOf»?
Memorizzi uno stato che sopravvive alla ricomposizione. Eserciti Jetpack Compose Academy con codice pratico che esegui direttamente nel browser, e un tutor IA 24/7 risponde alle tue domande mentre lavori sulla lezione.
Ho bisogno di esperienza per iniziare Jetpack Compose Academy?
Non è richiesta alcuna esperienza precedente. Jetpack Compose Academy su CoddyKit è strutturato per principianti e studenti avanzati, quindi puoi iniziare da qui o dall'inizio e procedere al tuo ritmo. Questa è la lezione 1 di 4.
Quanto tempo richiede la lezione «remember e mutableStateOf»?
La maggior parte delle lezioni CoddyKit richiede circa 5–10 minuti. Ogni lezione è breve e interattiva, quindi fai progressi costanti e riprendi esattamente da dove hai lasciato su web e app.
Posso scrivere ed eseguire codice in questa lezione Jetpack Compose Academy?
Sì. Ogni lezione Jetpack Compose Academy include un editor di codice integrato, quindi scrivi ed esegui codice reale direttamente nel tuo browser e ricevi feedback istantaneo dall'IA — nessuna configurazione locale necessaria.
Tutte le lezioni di questo corso
- remember e mutableStateOf
- Ricomposizione: cosa causa un ridisegno
- Creare un contatore di tocchi
- rememberSaveable dopo la rotazione