pub fn use_state<T: 'static, F: FnOnce() -> T>(initial_state_fn: F) -> UseStateHandle<T> {
    use_hook(
        // Initializer
        move || UseState {
            current: Rc::new(initial_state_fn()),
        },
        // Runner
        move |hook, updater| {
            let setter: Rc<(dyn Fn(T))> = Rc::new(move |new_val: T| {
                updater.callback(move |st: &mut UseState<T>| {
                    st.current = Rc::new(new_val);
                    true
                })
            });

            let current = hook.current.clone();
            UseStateHandle {
                value: current,
                setter,
            }
        },
        // Destructor
        |_| {},
    )
}
/// This hook is used to mange state in a function component.
///
/// # Example
/// ```rust
/// # use yew::prelude::*;
/// # use std::rc::Rc;
/// #
/// #[function_component(UseState)]
/// fn state() -> Html {
///     let counter = use_state(|| 0);
///     let onclick = {
///         let counter = counter.clone();
///         Callback::from(move |_| counter.set(*counter + 1))
///     };
///
///
///     html! {
///         <div>
///             <button {onclick}>{ "Increment value" }</button>
///             <p>
///                 <b>{ "Current value: " }</b>
///                 { *counter }
///             </p>
///         </div>
///     }
/// }
/// ```
This hook is used to mange state in a function component.
This hook is used to mange state in a function component .
pub fn use_state<T: 'static, F: FnOnce() -> T>(initial_state_fn: F) -> UseStateHandle<T> {
    use_hook(
        // Initializer
        move || UseState {
            current: Rc::new(initial_state_fn()),
        },
        // Runner
        move |hook, updater| {
            let setter: Rc<(dyn Fn(T))> = Rc::new(move |new_val: T| {
                updater.callback(move |st: &mut UseState<T>| {
                    st.current = Rc::new(new_val);
                    true
                })
            });
            let current = hook.current.clone();
            UseStateHandle {
                value: current,
                setter,
            }
        },
        // Destructor
        |_| {},
    )
}
pub fn use_state < T : ' static , F : FnOnce ( ) -> T > ( initial_state_fn : F ) -> UseStateHandle < T > { use_hook ( // Initializer move | | UseState { current : Rc :: new ( initial_state_fn ( ) ) , } , // Runner move | hook , updater | { let setter : Rc < ( dyn Fn ( T ) ) > = Rc :: new ( move | new_val : T | { updater . callback ( move | st : & mut UseState < T > | { st . current = Rc :: new ( new_val ) ; true } ) } ) ; let current = hook . current . clone ( ) ; UseStateHandle { value : current , setter , } } , // Destructor | _ | { } , ) }
This hook is used to mange state in a function component.
This hook is used to mange state in a function component .
initial_state_fn
F
initial_state_fn
F
example
None
use yew::prelude::*;
None
use std::rc::rc;
None
[function_component(usestate)]
fn state() -> Html {
let counter = use_state(|| 0);
let onclick = {
let counter = counter.clone();
Callback::from(move |_| counter.set(*counter + 1))
}.

html.
fn state () - > Html { let counter = use_state ( || 0 ) ; let onclick = { let counter = counter . clone () ; Callback :: from ( move |_| counter . set ( * counter + 1 )) } . html .