pub fn useState<T: Clone + 'static>(init: impl FnOnce() -> T) -> MutableState<T>Expand description
A composable hook that creates and remembers a MutableState.
This is the primary way to define local state in a composable function.
It combines remember and mutableStateOf.
§Arguments
init- A closure that provides the initial value. This is only called once when the composable enters the composition.
§Example
ⓘ
#[composable]
fn Counter() {
// "count" persists across recompositions.
// If we used mutableStateOf directly, it would reset to 0 every frame.
let count = useState(|| 0);
Button(
onClick = move || count.set(count.value() + 1),
|| Text(format!("Count: {}", count.value()))
);
}