Function dioxus_hooks::use_state
source · pub fn use_state<T: 'static>(
cx: &ScopeState,
initial_state_fn: impl FnOnce() -> T
) -> &UseState<T>
Expand description
Store state between component renders.
Dioxus equivalent of useState, designed for Rust
The Dioxus version of useState
for state management inside components. It allows you to ergonomically store and
modify state between component renders. When the state is updated, the component will re-render.
ⓘ
const Example: Component = |cx| {
let count = use_state(cx, || 0);
cx.render(rsx! {
div {
h1 { "Count: {count}" }
button { onclick: move |_| *count.modify() += 1, "Increment" }
button { onclick: move |_| *count.modify() -= 1, "Decrement" }
}
))
}