pub fn use_hook<State>(initializer: impl FnOnce() -> State) -> Statewhere
State: Clone + 'static,
Expand description
Store a value between renders. The foundational hook for all other hooks.
Accepts an initializer
closure, which is run on the first use of the hook (typically the initial render). The return value of this closure is stored for the lifetime of the component, and a mutable reference to it is provided on every render as the return value of use_hook
.
When the component is unmounted (removed from the UI), the value is dropped. This means you can return a custom type and provide cleanup code by implementing the Drop
trait
§Example
use dioxus_core::use_hook;
// prints a greeting on the initial render
pub fn use_hello_world() {
use_hook(|| println!("Hello, world!"));
}